我有3个表:pics
,folders
和pics_by_folders
。
图片
pic_ID | name | info
文件夹
folder_ID | name | info
pics_by_folders
folder_ID | pic_ID
我正在尝试返回符合要求x,y,z的所有pics
,但前提是它们包含在满足要求j,k,l的文件夹中。我希望我能以一种有道理的方式解释这一点。我目前的陈述是:
SELECT * FROM pics WHERE
pic_ID = 'x' AND
pics.name = 'y' AND
pics.info = 'Z'
JOIN pics_by_folder as pbf ON
pbf.pic_ID = pics.pic_ID
JOIN folders WHERE
folder_ID = 'j' AND
folders.name = 'k' AND
folders.info = 'l'
ON
folders.folder_ID = pbf.folder_ID
这不起作用,我认为它与语句的排序有关,或者sql不允许"JOIN x WHERE ..."
语句。无论哪种方式,任何帮助将不胜感激。
答案 0 :(得分:2)
语法错误!!!。
SELECT
*
FROM pics
JOIN pics_by_folder as pbf ON pbf.pic_ID = pics.pic_ID
JOIN folders ON folders.folder_ID = pbf.folder_ID
WHERE folder_ID = 'j' AND
folders.name LIKE 'k' AND
folders.info LIKE 'l' AND
pic_ID LIKE 'x' AND
pics.name LIKE 'y' AND
pics.info LIKE 'Z'
有些说明:
请始终指定结果集中您想要的列。否则你会自己感到困惑,因为你不知道结果集中会有多少列。
folders.name LIKE 'k'
相当于folders.name = 'k'
如果您想要完全匹配,请使用folders.name ='k'
,否则请使用下面的通配符运算符%
:
folders.name LIKE '%k%'
答案 1 :(得分:0)
目前还不清楚你想做什么。但你试过吗。
SELECT * FROM pics WHERE
pic_ID LIKE 'x' AND
pics.name LIKE 'y' AND
pics.info LIKE 'Z'
JOIN pics_by_folder as pbf ON
pbf.pic_ID = pics.pic_ID
JOIN folders on folders.folder_ID = pbf.folder_ID and
folder_ID = 'j' AND
folders.name LIKE 'k' AND
folders.info LIKE 'l'
答案 2 :(得分:0)
你是对的,这是错误的sintaxis
检查 manual
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references] --------------- THIS INCLUDE ALL JOIN
[WHERE where_condition] --------------- ALL RESTRICTION TOGETHER
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
答案 3 :(得分:0)
你可以写这样的语句
select * from pics where pic_id like 'x' and pics.name like 'y' and
pics.info like 'z' and pic_id in
(select pic_id from pics_by_folders where folder_id in
(select folder_id from folders where folder_id ='j' and
folders.name like 'k' and folders.info like 'l'))