Select ? Inner join

Discussion in 'MySQL' started by webboy, Jul 10, 2011.

  1. #1
    Hi all , I wish to select all the rows from one table that matches a cell in another table , but when i do it it a get duplicate rows

    eg i want to select all the rows from T1 that match the word A in table T2

    so the result would be a,a,a, or if it was C i would get one C



    eg +--------+ +--------+
    | T1 | |T2 |
    +--------+ +--------+
    |A | |A |
    |A | |C |
    |A | | C |
    |C | | C |



    What would be the SELECT or the join query to do this ?
     
    webboy, Jul 10, 2011 IP
  2. annie0207

    annie0207 Greenhorn

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    In my option, you may try DISTINCT keyword in SELECT statement and INNER JOIN query ;)
     
    annie0207, Jul 11, 2011 IP
  3. webboy

    webboy Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    how so could you give an example
     
    webboy, Jul 11, 2011 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    You are also going to need a sub-query:


    SELECT T1.fa
    FROM T1 
    INNER JOIN (SELECT DISTINCT T2.fb FROM T2) AS SUB ON T1.fa = SUB.fb;
    
    PHP:
    T1 is the first table, fa is the field in that table whose results you want.
    T2 is the second table, fb is the field in that table you want to limit results to.
     
    plog, Jul 11, 2011 IP