How to match only a single value in field with multiple values?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nawedita
    New Member
    • Mar 2010
    • 6

    How to match only a single value in field with multiple values?

    hi everyone

    I have one user table where userid store and another booking table where i store userid like(3,4,5,8)th is for only some user who have access to show booing records.
    if userid is '5' how i fetch only those record which match on booking table(userid).



    please help me.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Could you show us the exact structure of these tables?
    Also, which database system is this? MySQL?

    Have you tried simply:
    [code=sql]SELECT * FROM booking
    WHERE userid = 5[/code]

    Comment

    • nawedita
      New Member
      • Mar 2010
      • 6

      #3
      user table [uid, name]
      Booking table [id,userid,recor d]
      i have used mysql database.
      In booking table (id,userid,reco rd)values insert as ('1','1,4,5,6', 'text')...
      if uid is '5' then how i fetch only those record which match on booking table(userid).

      Comment

      • pod
        Contributor
        • Sep 2007
        • 298

        #4
        Try Atli' s code and I strongly suggest you read up on SQL as well, check this site tutorial:

        W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
        Last edited by pod; Sep 27 '10, 01:04 PM. Reason: typo in the url

        Comment

        • nawedita
          New Member
          • Mar 2010
          • 6

          #5
          This query is not working.
          SELECT * FROM `ebooking` WHERE `userid`='5'

          Because i want to match only one id ('1,4,5,6').

          Comment

          • pod
            Contributor
            • Sep 2007
            • 298

            #6
            If I read you right, you have a list of IDs in each record:

            booking table (id,userid,reco rd)
            where "id" is a unique ID for the record
            and "userid" is a list of uid from your user table


            this is not a good database design, therefore it complicates your queries a bit but there is a solution although a bit heavy:

            Code:
            SELECT * FROM booking 
            WHERE userid = '5' 
            OR userid LIKE '5,%' 
            OR userid LIKE '%,5' 
            OR userid LIKE '%,5,%'
            where "%" is a wildcard

            this will work for a query looking for one userid but when looking for multiple userids, it will get heavier and more complicated

            ---------------

            you should consider the following database design:
            USER table [uid, name]
            BOOKING table [id,record] where each record contains one booking with its unique ID
            USER_BOOKING table [id,uid]where each record contains a user ID and a booking ID

            this last table could have multiple user IDs for the same booking ID
            ...
            look up some good relational database tutorial, it will really help understanding SQL and all
            Last edited by pod; Sep 27 '10, 01:48 PM. Reason: clearing up a little

            Comment

            Working...