简体   繁体   English

如何连接表以返回所有正确的行,以及另外一个空行?

[英]How can I join a table to return all rows right and in addition a null row?

I have two tables, one with a list of parts, and another with a list of attributes. 我有两个表,一个带有零件列表,另一个带有属性列表。 What I'm trying to achieve is to list all parts with their attributes, and also the base row itself a la: 我想要实现的是列出所有具有其属性的部分,以及基本行本身的la:

Part   Attribute
----------------
Cake   Batman
Cake   Princess
Cake   Spiderman
Cake   NULL

QUERY
----------------
SELECT p.Name, pa.Name
FROM PartsTbl p
LEFT JOIN PartAttrib pa ON p.Name= pa.BaseName
WHERE p.Name = 'Cake'

Currently, this only returns everything except the last row. 当前,这仅返回除最后一行以外的所有内容。 How can I produce the last row as part of the same query, instead of by running a second query? 如何在同一查询中生成最后一行,而不是通过运行第二个查询?

UPDATE --------------- 更新---------------

Table Structure
PartsTbl - Name
PartAttrib - BaseName (To PartsTbl.Name), Name

A simple Union is your friend :o) 一个简单的联盟是你的朋友:o)

SELECT p.Name, pa.Name
FROM PartsTbl p
LEFT JOIN PartAttrib pa ON p.Name= pa.BaseName
WHERE p.Name = 'Cake'
UNION
SELECT Name, NULL
FROM PartsTbl
WHERE Name = 'Cake'

暂无
暂无

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

相关问题 MySQL JOIN:仅返回右表中所有值均为 NULL 的行 - MySQL JOIN: Only return rows where all values from right table are NULL 左外连接除匹配行外,还返回空的右侧 - Left Outer Join That Returns Null Right Side In Addition To The Matching Rows 如何加入左表可以为空或右表可以为空的多对多? - How do I join a many-to-many where the left table can be null OR the right table can be null? 如何从左表返回行,其中右表的条件在没有子查询的所有行上为真 - How to return rows from left table where condition on right table true on all row without sub query MySQL左连接右侧多行,如何将左侧的每一行合并为一行(我想要从右侧表中连接的字段)? - MySQL left join with multiple rows on right, how to combine to one row for each on left (with the field I want from the right table concated)? 如何返回所有包含空外键值的联接表 - how to return all join table including null foreign key values 当右表中没有对应的行时,左JOIN返回ALL NULL值 - Left JOIN returning ALL NULL values when no corresponding row in right table sql右联接从右表返回多行 - sql right join return multiple rows from right table 左外部联接,带选择,其中右表中的值不返回左中的所有行 - Left outer join with select where value in right table does not return all rows from left 如何调整此3表MySQL连接查询以消除包含空值列的行? - How can I adjust this 3 Table MySQL join query to eliminate rows that contain a null value column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM