MySql Inner Join with WHERE 子句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12364602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MySql Inner Join with WHERE clause
提问by Aditya Kumar
Here is my code:
这是我的代码:
SELECT table1.f_id FROM table1 WHERE table1.f_com_id = '430' AND
table1.f_status = 'Submitted'
INNER JOIN table2
ON table2.f_id = table1.f_id
where table2.f_type = 'InProcess'
I need information from table1
as all the id associated with f_com_id
as 430 and status as submitted and the type should be only in process which is stored in other table(table2
)
我需要来自table1
与f_com_id
430相关联的所有 id和已提交状态的信息,并且该类型应该仅在存储在其他表中的过程中(table2
)
f_id
is p_key
and f_key
in both the tables.
But this giving me errors, I think I am placing the WHERE
clause wrong, how to fix it.?
f_id
是p_key
和f_key
在两个表中。
但这给了我错误,我想我把WHERE
条款放错了,如何解决。?
Error msg: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN table2 ON table2.f_id = ' at line 2
错误消息:#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 2 行的 'INNER JOIN table2 ON table2.f_id = ' 附近使用的正确语法
回答by hims056
Yes you are right. You have placed WHERE
clause wrong. You can only use one WHERE
clause in single query so try AND
for multiple conditions like this:
你是对的。你把WHERE
子句放错了。您只能WHERE
在单个查询中使用一个子句,因此请尝试AND
使用多个条件,如下所示:
SELECT table1.f_id FROM table1
INNER JOIN table2
ON table2.f_id = table1.f_id
WHERE table2.f_type = 'InProcess'
AND f_com_id = '430'
AND f_status = 'Submitted'
回答by Mistdemon
1. Change the INNER JOIN before the WHERE clause.
2. You have two WHEREs which is not allowed.
1. 更改 WHERE 子句之前的 INNER JOIN。
2. 你有两个不允许的 WHERE。
Try this:
尝试这个:
SELECT table1.f_id FROM table1 INNER JOIN table2 ON (table2.f_id = table1.f_id AND table2.f_type = 'InProcess') WHERE table1.f_com_id = '430' AND table1.f_status = 'Submitted'
SELECT table1.f_id FROM table1 INNER JOIN table2 ON (table2.f_id = table1.f_id AND table2.f_type = 'InProcess') WHERE table1.f_com_id = '430' AND table1.f_status = 'Submitted'
回答by xdazz
You could only write one where clause.
你只能写一个 where 子句。
SELECT table1.f_id FROM table1
INNER JOIN table2
ON table2.f_id = table1.f_id
where table1.f_com_id = '430' AND
table1.f_status = 'Submitted' AND table2.f_type = 'InProcess'
回答by AboQutiesh
You are using two WHERE
clauses but only one is allowed. Use it like this:
您使用了两个WHERE
子句,但只允许使用一个。像这样使用它:
SELECT table1.f_id FROM table1
INNER JOIN table2 ON table2.f_id = table1.f_id
WHERE
table1.f_com_id = '430'
AND table1.f_status = 'Submitted'
AND table2.f_type = 'InProcess'