简体   繁体   English

我如何总是从一个表中提取数据,又如何从第二个表中提取数据(如果MySQL中存在的话)?

[英]How do I always pull data from one table, but also pull data from a second table if it's there in MySQL?

Here are my two tables: 这是我的两张桌子:

oasis 绿洲

+-----+-------+
| id  | title | 
+-------------+
| 234 |   a   | 
| 235 |   b   | 
| 236 |   c   | 
+-----+-------+

user_collection user_collection

+----+---------+----------+------+
| id | oasisid | username | data |
+--------------+----------+------+
| 1  |   234   |    joe   | blah |
| 2  |   235   |    bob   | blah |
| 3  |   236   |    ted   | blah |
+----+---------+----------+------+

Here's my query: 这是我的查询:

SELECT *
FROM oasis
JOIN user_collection ON oasis.id = user_collection.oasisid
WHERE username = 'greg'
AND oasis.id = '234'

What I want to do here is pull everything from oasis and user_collection that match, but also pull the information from oasis even if there is NO match on user_collection . 我想在这里做的是从绿洲user_collection那场比赛拉的一切,而且还拉离绿洲的信息即使在user_collection不匹配。

How do I fix my query to accomplish this? 我该如何解决查询问题呢?

That's what the manual says, just to stick to MySQL... "If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find rows in a table that have no counterpart in another table: 这就是手册所说的,只是坚持使用MySQL ...“如果在ON或LEFT JOIN的USING部分中没有正确的表匹配行,则将所有列设置为NULL的行用于正确的表您可以使用此事实在一个表中查找在另一个表中没有对应项的行:

SELECT left_tbl.*
  FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id
 WHERE right_tbl.id IS NULL;"

http://dev.mysql.com/doc/refman/5.0/en/join.html http://dev.mysql.com/doc/refman/5.0/en/join.html

It also makes it clear, why to move the username criteria from the where clause to the ON clause, as Andrew says... 这也很清楚,为什么要将用户名条件从where子句移动到ON子句,就像Andrew所说的...

You want a left join , but you have to be careful about the conditions in the where clause. 您需要left join ,但是必须注意where子句中的条件。 Conditions on the second table need to be in the on clause for the left join to work: 为了使left join正常工作, 第二个表上的条件必须位于on子句中:

SELECT *
FROM oasis JOIN
     user_collection
     ON oasis.id = user_collection.oasisid and user_collection.username = 'greg'
WHERE oasis.id = '234';

Conditions on the first table should go in the where clause. 第一个表上的条件应放在where子句中。

 SELECT s.ome
      , c.olumns
   FROM table1 s
   LEFT 
   JOIN table2 c
     ON c.some_id = s.some_id
    AND c.some_column = 'one thing'
  WHERE s.some_other_column = 'another thing'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从一个表中提取数据,并将其插入到另一个表中的列? - How do i pull data from one table and insert it into a column in another table? MySQL子查询从另一个表中提取数据 - MySql subquerying to pull data from another table 从一个表中选择数据,检查并从第二个表中提取值 - select data from one table, check against and pull values from second table 如何从堆栈中的2个表中提取数据 - How to pull data from 2 table in stack PHP从一个表中获取ID,并从另一表中提取与这些ID相对应的数据 - PHP take id's from one table and pull data that corresponds with those id's from another table 如何使用一行从同一mySQL表中提取其他多行 - How do I use one row to pull multiple other rows from the same mySQL table 没有提示错误-无法从第二个表中提取数据 - No errors prompted - cannot pull data from second table 当相关记录在MySQL中包含给定值时,如何从联接表中提取所有数据? - How do you pull all data from a joined table when a related record contains a given value in MySQL? 从一个表存储到另一表中的记录中获取附加数据 - Pull record from one table store in another table with additional data 如何根据第二秒中数据的存在从mySQL数据库的一个表中选择一条记录? - How do I select a record from one table in a mySQL database, based on the existence of data in a second?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM