简体   繁体   English

mysql join 2 个表 - 显示一个表中的所有行

[英]mysql join 2 tables - show all rows from one table

I have two tables, like so: table "a" contains:我有两个表,如下所示:表“a”包含:

id|name

stock1|fullname
stock2|fullname2
stock3|fullname3

table "b" contains product quantities for given stock.表“b”包含给定库存的产品数量。

id|stock_id|quantity|product_id|
1|stock1|3|13
2|stock3|4|13
3|stock1|1|5
4|stock2|2|2

Now I would need to combine those two tables, so that each product takes its stock full name from table "a", and if its quanitity is not given for stock, it would still show the row with the quanitity as 0.现在我需要组合这两个表,以便每个产品从表“a”中获取其库存全名,如果没有为库存提供其数量,它仍将显示数量为 0 的行。

So from my example, product_id 13 would show as:因此,从我的示例中,product_id 13 将显示为:

stock|quanitity|product_id|stock_fullname
stock1|3|13|fullname1
stock2|0|13|fullname2
stock3|4|13|fullname3

You should be able to use a LEFT JOIN to achieve this.您应该能够使用 LEFT JOIN 来实现这一点。

SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
  ON a.id = b.stock_id
  AND b.product_id = 13

It sounds like you need to use a LEFT JOIN , although the records with no quantity might show as NULL rather than zero.听起来您需要使用LEFT JOIN ,尽管没有数量的记录可能显示为 NULL 而不是零。 Something like:就像是:

SELECT a.*, b.* 
FROM table_a a
    LEFT JOIN table_b b ON a.stock_id = b.stock_id

You should be able to use a LEFT JOIN to achieve this.您应该能够使用LEFT JOIN来实现这一点。

SELECT a.id AS stock, COALESCE(b.quanitity,0), b.product_id, a.name AS stock_fullname
FROM a
LEFT JOIN b
  ON a.id = b.stock_id
  AND b.product_id = 13

尝试这个:

SELECT stock,COALESCE(quanitity,0),product_id,stock_fullname FROM stock JOIN product 

I think this query should work for your example:我认为此查询应该适用于您的示例:

SELECT a.id stock if(b.quantity IS NULL, 0, b.quantity), 
       b.product_id, a.name stock_fullname
FROM b
LEFT JOIN a b.stock = a.id
WHERE b.product_id = 13;

You need an outer join so that rows from the a table without a corresponding row in b are still considered.您需要一个外部联接,以便仍然考虑 a 表中没有 b 中相应行的行。 An inner join, by contrast, insists that you have a matching row.相比之下,内部联接坚持您有一个匹配的行。 If you are pulling a value from the table where you don't have a row, you get NULL.如果从没有行的表中提取值,则会得到 NULL。 Syntax varies between DBs and there is a distinction made depending on if it's the table on the left or right that gets the fake rows. DB 之间的语法有所不同,并且根据是左侧还是右侧的表获取假行而有所不同。

see other answers for syntax.请参阅语法的其他答案。

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

相关问题 MySQL \\联接2个表并在一列中显示2行的结果 - MySQL \ join 2 tables and show results from 2 rows in one column MySQL查询 - 连接表以显示一个表中的所有值,并仅匹配其他列中的结果 - MySQL Query - join tables to show all values from one table and only matching results in other column mysql加入2个表并计算每个表中的所有行 - mysql join 2 tables and count all rows from each table 在mysql中联接三个表,但所有数据都来自一个表 - Join three tables in mysql but have all data from one table MYSQL QUERY LEFT JOIN显示一个表中的所有数据 - MYSQL QUERY LEFT JOIN SHOW ALL DATA FROM ONE TABLE mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行 - mysql join query for select rows from two tables that one table is having multiple rows matching to the first table MySQL联接3个表并在一个表中查找“缺失”行 - MySQL join 3 tables and find “missing” rows in one table MySQL连接两个表,一个表具有多个匹配行 - mysql join two tables that one table is having multiple matching rows MySQL左连接问题与3个表不返回第一个表的所有行 - Mysql left join issue with 3 tables not returning all rows from first table 加入MySQL表:在左表的一行中显示右表的所有结果 - Join MySQL Tables: Display All Results From Right Table In One Row Of Left Table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM