开发者

how to use inner join in my condition

开发者 https://www.devze.com 2023-02-20 15:16 出处:网络
I have two tables like these c_IDname --------- 7a 6a 5a 4d AND c_IDphoto ----------- 71 61 50 4开发者_如何转开发1

I have two tables like these

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

AND

   c_ID    photo
  -----   ------
    7       1
    6       1
    5       0
    4     开发者_如何转开发  1

How can i select records that name is a and photo is 1 ?

Thanks


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  


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

It is good practice not to use upper case characters in databases.


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

This being said, the way your example looks maybe you could normalize your two tables into one table with the fields c_ID, name and photo


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


Try this:

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
0

精彩评论

暂无评论...
验证码 换一张
取 消