[英]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.