Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Use Conditions In Query [MySQL]

Use Conditions In Query [MySQL]

Scheduled Pinned Locked Moved Database
databasemysqlhelpquestion
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Django_Untaken
    wrote on last edited by
    #1

    Hello there. First let me give the 3 tables' definitions then I ask question. Users UserId UserName Members MemberId MemberName Orders OrderId UserId IsUserMember ItemName In 3rd table, you can see that I am using boolean column which tells whether this Order belongs to guest user or member user. I want to select corresponding data using ONE QUERY. Here is what I have tried so far but it is far from working SELECT O.*, CASE WHEN O.IsUserMember = 1 THEN U.UserName ELSE M.MemberName END FROM Orders O, CASE WHEN O.IsUserMember = 1 THEN Users U ELSE Members M END WHERE CASE WHEN O.IsUserMember = 1 THEN O.UserId = U.UserId ELSE B.UserId = M.MemberId END; It is giving me this following error: Syntax error near 'CASE WHEN B.IsThisUserMember = 1 THEN Users U ELSE Members M END WHERE CASE WHEN' at line 1 NOTE: If I remove first CASE condition, then I get empty set. Thanks for whatever you share.

    Z Richard DeemingR P 3 Replies Last reply
    0
    • D Django_Untaken

      Hello there. First let me give the 3 tables' definitions then I ask question. Users UserId UserName Members MemberId MemberName Orders OrderId UserId IsUserMember ItemName In 3rd table, you can see that I am using boolean column which tells whether this Order belongs to guest user or member user. I want to select corresponding data using ONE QUERY. Here is what I have tried so far but it is far from working SELECT O.*, CASE WHEN O.IsUserMember = 1 THEN U.UserName ELSE M.MemberName END FROM Orders O, CASE WHEN O.IsUserMember = 1 THEN Users U ELSE Members M END WHERE CASE WHEN O.IsUserMember = 1 THEN O.UserId = U.UserId ELSE B.UserId = M.MemberId END; It is giving me this following error: Syntax error near 'CASE WHEN B.IsThisUserMember = 1 THEN Users U ELSE Members M END WHERE CASE WHEN' at line 1 NOTE: If I remove first CASE condition, then I get empty set. Thanks for whatever you share.

      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #2

      I don't do MySql but it should probably be like this:

      WHERE (O.UserId = U.UserId AND O.IsUserMember = 1) OR (B.UserId = M.MemberId AND O.IsUserMember = 0)

      There are only 10 types of people in the world, those who understand binary and those who don't.

      1 Reply Last reply
      0
      • D Django_Untaken

        Hello there. First let me give the 3 tables' definitions then I ask question. Users UserId UserName Members MemberId MemberName Orders OrderId UserId IsUserMember ItemName In 3rd table, you can see that I am using boolean column which tells whether this Order belongs to guest user or member user. I want to select corresponding data using ONE QUERY. Here is what I have tried so far but it is far from working SELECT O.*, CASE WHEN O.IsUserMember = 1 THEN U.UserName ELSE M.MemberName END FROM Orders O, CASE WHEN O.IsUserMember = 1 THEN Users U ELSE Members M END WHERE CASE WHEN O.IsUserMember = 1 THEN O.UserId = U.UserId ELSE B.UserId = M.MemberId END; It is giving me this following error: Syntax error near 'CASE WHEN B.IsThisUserMember = 1 THEN Users U ELSE Members M END WHERE CASE WHEN' at line 1 NOTE: If I remove first CASE condition, then I get empty set. Thanks for whatever you share.

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        You're looking for a LEFT JOIN:

        SELECT
        O.*,
        CASE
        WHEN O.IsUserMember = 1 THEN U.UserName
        ELSE M.MemberName
        END As Name
        FROM
        Orders O
        LEFT JOIN Users U
        ON U.UserId = O.UserId And O.IsUserMember = 0
        LEFT JOIN Members M
        ON M.MemberId = O.UserId And O.IsUserMember = 1
        ;


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        D 1 Reply Last reply
        0
        • D Django_Untaken

          Hello there. First let me give the 3 tables' definitions then I ask question. Users UserId UserName Members MemberId MemberName Orders OrderId UserId IsUserMember ItemName In 3rd table, you can see that I am using boolean column which tells whether this Order belongs to guest user or member user. I want to select corresponding data using ONE QUERY. Here is what I have tried so far but it is far from working SELECT O.*, CASE WHEN O.IsUserMember = 1 THEN U.UserName ELSE M.MemberName END FROM Orders O, CASE WHEN O.IsUserMember = 1 THEN Users U ELSE Members M END WHERE CASE WHEN O.IsUserMember = 1 THEN O.UserId = U.UserId ELSE B.UserId = M.MemberId END; It is giving me this following error: Syntax error near 'CASE WHEN B.IsThisUserMember = 1 THEN Users U ELSE Members M END WHERE CASE WHEN' at line 1 NOTE: If I remove first CASE condition, then I get empty set. Thanks for whatever you share.

          P Offline
          P Offline
          phil o
          wrote on last edited by
          #4

          I have got a side-question: shouldn't IsUserMember be a column in the Users table rather than in the Orders one? Is it possible that a specific user is considered as a member for an order, and not a member for another one? (just curious)

          Loneliness and cheeseburgers are a dangerous mix.

          D 1 Reply Last reply
          0
          • P phil o

            I have got a side-question: shouldn't IsUserMember be a column in the Users table rather than in the Orders one? Is it possible that a specific user is considered as a member for an order, and not a member for another one? (just curious)

            Loneliness and cheeseburgers are a dangerous mix.

            D Offline
            D Offline
            Django_Untaken
            wrote on last edited by
            #5

            phil.o wrote:

            Is it possible that a specific user is considered as a member for an order, and not a member for another one?

            Users represent guest users. Members represent registered users. There are online businesses out there which allow you to shop either as guest user or first register and then continue as member. Having said this, I am not a DB guru. I just started working in it with more serious attitude. Any flaw you see in my design, please let me know.

            P 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              You're looking for a LEFT JOIN:

              SELECT
              O.*,
              CASE
              WHEN O.IsUserMember = 1 THEN U.UserName
              ELSE M.MemberName
              END As Name
              FROM
              Orders O
              LEFT JOIN Users U
              ON U.UserId = O.UserId And O.IsUserMember = 0
              LEFT JOIN Members M
              ON M.MemberId = O.UserId And O.IsUserMember = 1
              ;


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              D Offline
              D Offline
              Django_Untaken
              wrote on last edited by
              #6

              When putting this code section, what language you used in PRE tag? I could not find out how to proper format and paste SQL queries in codeproject. thnx

              Richard DeemingR 1 Reply Last reply
              0
              • D Django_Untaken

                phil.o wrote:

                Is it possible that a specific user is considered as a member for an order, and not a member for another one?

                Users represent guest users. Members represent registered users. There are online businesses out there which allow you to shop either as guest user or first register and then continue as member. Having said this, I am not a DB guru. I just started working in it with more serious attitude. Any flaw you see in my design, please let me know.

                P Offline
                P Offline
                phil o
                wrote on last edited by
                #7

                Django_Untaken wrote:

                Users represent guest users. Members represent registered users.

                But, at the end, they are all users, whether they are registered or not. You do not have any "Members" table. Is it possible that a specific user (so, only one user id), can order as registered user once, and then as unregistered user later (or the contrary)? Again, I do not know the details of your requirements; I may be wrong, as I may miss something important. Kindly :)

                Loneliness and cheeseburgers are a dangerous mix.

                1 Reply Last reply
                0
                • D Django_Untaken

                  When putting this code section, what language you used in PRE tag? I could not find out how to proper format and paste SQL queries in codeproject. thnx

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  <pre lang="SQL">
                  ...
                  </pre>


                  "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups