简体   繁体   English

连接两个表时WHERE子句不显示所有行? 我需要左连接吗?

[英]WHERE clause not showing all rows when joining two tables? Do I need a left join?

I'm not sure what the title is for this question. 我不确定这个问题的标题是什么。

Updated to show more concrete problem. 更新以显示更具体的问题。

I have 2 tables. 我有2张桌子。

table_a 表-A

+------------+--------+---------+
| date       | used   | solar   |
+------------+--------+---------+
| 2012-03-16 | 20.317 |  -4.167 |
| 2012-03-17 | 17.308 | -38.593 |
| 2012-03-18 |  9.529 | -42.414 |
| 2012-03-19 | 13.266 | -26.372 |
| 2012-03-20 |  7.077 | -41.742 |
| 2012-03-21 |  7.625 | -43.348 |
| 2012-03-22 | 10.943 | -42.025 |
| 2012-03-23 | 11.362 | -38.532 |
| 2012-03-24 | 11.492 |  -9.434 |
| 2012-03-25 | 16.133 |  -8.280 |
| 2012-03-26 | 17.054 | -31.340 |
| 2012-03-27 |  9.356 | -49.759 |
| 2012-03-28 | 11.984 | -16.299 |
| 2012-03-29 | 20.175 |  -6.412 |
| 2012-03-30 | 16.024 | -47.509 |
| 2012-03-31 | 10.605 | -16.102 |
+------------+--------+---------+

table_b 表-B

+------------+--------+
| date       | hdd    |
+------------+--------+
| 2012-03-01 | 32.884 |
| 2012-03-02 | 31.402 |
| 2012-03-03 | 22.487 |
| 2012-03-04 | 31.087 |
| 2012-03-05 | 41.679 |
| 2012-03-06 | 39.892 |
| 2012-03-07 | 17.626 |
| 2012-03-08 |  8.433 |
| 2012-03-09 | 29.580 |
| 2012-03-10 | 35.238 |
| 2012-03-11 | 22.453 |
| 2012-03-12 | 11.724 |
| 2012-03-13 |  6.486 |
| 2012-03-14 | 16.140 |
| 2012-03-15 | 21.425 |
| 2012-03-16 | 20.486 |
| 2012-03-17 | 13.744 |
| 2012-03-18 |  8.207 |
| 2012-03-19 |  3.878 |
| 2012-03-20 |  2.679 |
| 2012-03-21 |  2.701 |
| 2012-03-22 |  2.830 |
| 2012-03-23 |  4.219 |
| 2012-03-24 | 15.008 |
| 2012-03-25 | 19.155 |
| 2012-03-26 | 27.850 |
| 2012-03-27 | 32.985 |
| 2012-03-28 | 20.470 |
| 2012-03-29 | 26.230 |
| 2012-03-30 | 26.119 |
| 2012-03-31 | 24.353 |
+------------+--------+

table_a has 16 records. table_a有16条记录。 table_b has 31 records. table_b有31条记录。

If I use... 如果我用...

SELECT a.date, a.used, b.hdd
FROM table_a a RIGHT JOIN table_b b ON a.date=b.date
WHERE MONTH(a.date) = 3;

I only get 16 rows, presumably because table_a only has 16 rows. 我只得到16行,大概是因为table_a只有16行。 But actually it must be the where clause. 但实际上它必须是where子句。 How do I use the two together? 我如何一起使用这两个?

How do I get all 31 rows ( assuming dates match from each table for the other 16 rows ). 如何获得所有31行( 假设其他16行的每个表匹配日期 )。 I tried a left join but that didn't work. 我尝试了左连接,但是没有用。

SELECT a.date, a.used, b.hdd FROM table_a a
LEFT JOIN table_b b ON a.date=b.date;

use RIGHT JOIN instead since you want to show all 10 records from table_b 使用RIGHT JOIN代替,因为你想显示table_b所有10条记录

SELECT a.date, a.column1, b.column1 
FROM table_a a RIGHT JOIN table_b b ON a.date=b.date;

or interchange the tablenames so you can still use LEFT JOIN 或者交换表名,这样你仍然可以使用LEFT JOIN

SELECT b.date, b.column1, a.column1 
FROM table_b a LEFT JOIN table_a b ON a.date=b.date;

SIDENOTE 边注

The reason why you are only getting 5 records when you run this query, 您运行此查询时只获得5条记录的原因,

SELECT a.date, a.column1, b.column1 
FROM table_a a, table_b b 
WHERE a.date=b.date;

is because it performs like INNER JOIN 是因为它的表现就像INNER JOIN

SELECT a.date, a.column1, b.column1 
FROM table_a a INNER JOIN table_b b ON a.date=b.date

An INNER JOIN selects on rows that has atleast one match on the other table being joined. INNER JOIN选择在正在连接的另一个表上至少有一个匹配的行。 To fully understand about joins, please see the article below. 要完全了解联接,请参阅下面的文章。

Try this: 尝试这个:

SELECT a.date, a.used, b.hdd FROM table_b b
LEFT JOIN table_a a ON b.date=a.date AND MONTH(a.date) = 3;

OR 要么

SELECT a.date, a.used, b.hdd
FROM table_a a RIGHT JOIN table_b b ON a.date=b.date
WHERE MONTH(b.date) = 3;

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

相关问题 连接表,但不显示满足子句的行 - joining tables but not showing rows where clause is met 联接两个表时向左联接 - LEFT OUTER JOIN when joining two tables 有没有办法用“内连接”子句连接两个表,然后是多个“左连接”子句 - is there a way of joining two tables with the clause, “inner join,” then followed by multiple “left join” clause 左连接左边的所有行都不匹配Where子句 - Left Join with all rows from the left not matching the Where Clause 使用join和where子句从两个表中获取行 - Getting rows from two tables with a join and where clause 如何连接表,包括左表中的所有id,但只显示某些where子句的右表中的信息 - How to join tables including all ids from left table but only showing information from the right table given certain where clause 在对两个表进行LEFT JOIN后,如何用不满足WHERE子句中条件的NULL值保留行 - How to keep rows with NULL values that don't fulfill the condition in the WHERE clause after LEFT JOIN-ing two tables 当我只需要WHERE子句中的联接(右)表中的一列时,是否需要左外部联接? - Is a left outer join needed when I only need one column from the joined (right) table in the WHERE clause? 使用where子句条件在mysql中联接两个表 - Joining two tables in mysql with where clause condition 我可以不使用ON子句离开两个表吗? - Can I left join two tables without using ON clause?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM