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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 14:47:56  来源:igfitidea点击:

MySql Inner Join with WHERE clause

mysqlsqlwhere-clausemysql-error-1064

提问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 table1as all the id associated with f_com_idas 430 and status as submitted and the type should be only in process which is stored in other table(table2)

我需要来自table1f_com_id430相关联的所有 id和已提交状态的信息,并且该类型应该仅在存储在其他表中的过程中(table2

f_idis p_keyand f_keyin both the tables.
But this giving me errors, I think I am placing the WHEREclause wrong, how to fix it.?

f_idp_keyf_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 WHEREclause wrong. You can only use one WHEREclause in single query so try ANDfor 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 WHEREclauses 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'