如何在我的条件下使用内连接

时间:2011-03-27 13:25:14

标签: mysql sql

我有两张这样的表

   c_ID    name
    ---   ------
    7      a
    6      a
    5      a
    4      d

   c_ID    photo
  -----   ------
    7       1
    6       1
    5       0
    4       1

如何选择名称为a且照片为1的记录?

由于

5 个答案:

答案 0 :(得分:1)

select *  /*TODO: Add specific column(s) you want here*/
from   table1
       join table2
         on table1.c_ID = table2.c_ID
where  table1.name = 'a'
       and table2.photo = 1  

答案 1 :(得分:0)

SELECT * 
FROM table1 AS name LEFT JOIN table2 AS photo ON name.c_ID = photo.c_ID 
WHERE name.name = 'a' and photo.photo = 1

这就是说,你的例子看起来的方式也许你可以将你的两个表规范化为一个表格,其中包含字段c_ID,名称和照片

答案 2 :(得分:0)

试试这个:

SELECT table1.c_id, table1.name, table2.photo
  FROM table1 INNER JOIN table2
 ON table1.c_id = table2.c_id
   AND table1.name = 'a'
   AND table2.photo = 1

答案 3 :(得分:0)

SELECT t1.*, t2.*
FROM table1 t1
JOIN table2 t2 ON t2.c_ID = t1.c_ID
WHERE t1.name = 'a' AND t2.photo = 1

最好不要在数据库中使用大写字符。

答案 4 :(得分:0)

SELECT table1.c_ID, table1.name, table2.photo              // desired fields
FROM table1 INNER JOIN table2 ON table1.c_ID=table2.c_ID   // joining tables on common keys
WHERE table1.name='a' AND table2.photo=1;                  // desired condition