mysql连接逻辑运算符

时间:2013-12-15 20:06:36

标签: mysql join

我想仅使用JOIN进行逻辑操作,并获取所选表的所有行。

例如:

任务表:

+------+-------------+-----------+
| id   | name        | projectID |
+------+-------------+-----------+
| 1    | steal       | 3         |
| 2    | sell        | 2         |
| 3    | earn        | 1         |
+------+-------------+-----------+

项目表:

+------+-------------+-----------+
| id   | name        | online    |
+------+-------------+-----------+
| 1    | stealing    | 1         |
| 2    | selling     | 1         |
| 3    | earning     | 0         |
+------+-------------+-----------+

选择项目在线的所有任务(在线= 1)

我尝试了什么:

SELECT * 
FROM  `task` 
LEFT JOIN  `project` ON project.id = task.projectID && project.online =1

这会导致项目和任务中的所有行 但我只想要来自任务的行,并使用连接进行逻辑操作

3 个答案:

答案 0 :(得分:2)

使用*之前的表名仅选择特定表的行

SELECT `task`.* 
FROM  `task` 
INNER JOIN  `project` ON project.id = task.projectID 
                     AND project.online = 1

使用inner join。见this great explanation of joins

答案 1 :(得分:1)

尝试:

SELECT * 
FROM  `task` 
LEFT JOIN  `project` ON project.id = task.projectID WHERE project.online =1

答案 2 :(得分:0)

try this :

 SELECT T.* FROM  `task` T
 LEFT OUTER JOIN  `project` P ON P.id = T.projectID 
 WHERE P.online = 1