SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    5
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    match records with same value in a column

    Hi,

    Currently I have a simple table with 4 columns. A, B, C, D

    If I want to find all the records with the similar value in column B. How can i write a simple query to match this one ?

    Please advise. Thank you

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    This sounds like an sql question rather than a PHP one, you'd likely get a better response from someone over there (or the Mysql forum).

    Probably be a good idea of you could give a couple of examples of your criteria for "similar value" too.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,250
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT a,b,c,d
      FROM simpletable
     WHERE b = 'similar value'
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    5
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually what I want to know is how can I check whether 2 records has the same value under the same column.

    For instance.

    UserA and UserB as username in column A

    I want to check whether UserA and UserB has the same value in column B under the same table.

    How can I get a sql query to accomplish this in phpmysql?

    Thanks

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,250
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Code:
    SELECT a,b,c,d
      FROM simpletable
     WHERE b IN
           ( SELECT b
               FROM simpletable
             GROUP
                 BY b
             HAVING COUNT(*) > 1 )
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    5
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The query above is not working. =(

    What I need is to check whether UserA and UserB (which has their own unique ID in Column A) has the same transaction ID in column B.

    In short, I just want to compare 2 users ID which I already know and check whether have the same transaction ID in column B.

    Please advise and thanks.

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,250
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    what you just posted is not really the same problem as what you posted originally
    Code:
    SELECT CASE WHEN COUNT(*) = 1
                THEN 'has the same'
                ELSE 'has different'
            END AS result
      FROM ( SELECT b
               FROM simpletable 
              WHERE a = user1ID
             UNION
             SELECT b
               FROM simpletable 
              WHERE a = user2ID
           ) AS one_or_two
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    5
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    5
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, another issue that I need some advise.

    I try to run this one but it will show all the records with either one of the IDs entered.

    Code:
    $gotRec=mysql_query("SELECT `Rec_user_Rec_id` FROM f_Rec_users WHERE Rec_user_user_id ='A1'
    AND SELECT `Rec_user_Rec_id` FROM f_Rec_users WHERE Rec_user_user_id ='B2'") or die(mysql_error());
    	$numgotRec = mysql_num_rows($gotRec);
    		$gotRec2 = mysql_fetch_array($gotRec);
    The code above will show all the records where Rec_user_user_id = A1 or B2

    How can I make it only show the record in Rec_user_user_id (A1 and B2) that has the same Rec_user_Rec_id . Then I want to echo what is the same Rec_user_Rec_id found.

    Please advise and thank you.

  10. #10
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,250
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    that code will not run... you cannot say "AND SELECT"

    as for your question, i have no idea what you're asking
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Location
    5
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me rephrase what I am trying to do.

    - have a form to post 2 users ID

    - once I posted the 2 IDs, the query should check whether these 2 IDs have the same transaction code in the table.

    - If there is any, i want to fetch and print out the transaction code that both records have.

    that is what I am try to accomplish. Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •