[英]how to write mysql query with join and still display partial info if a record from one table is deleted?
i have a join query that displays info from 2 tables, but if a record is deleted from one of the tables, the joined record doesn't show. 我有一个显示来自2个表的信息的连接查询,但是如果从其中一个表中删除了一条记录,则不会显示已连接的记录。 i still want to display the joined record but with the data from the first table showing and the info with the second table missing. 我仍然希望显示已连接的记录,但显示第一个表中的数据和缺少第二个表的信息。
here is the query i'm working with. 这是我正在使用的查询。
$result = mysql_query("SELECT user_groups.*, pricing_groups.* FROM user_groups inner join pricing_groups on user_groups.pricing_group_id = pricing_groups.id LIMIT 2,18446744073709551615")
i'm limiting it by 2. i still want that in next version. 我将它限制在2.我仍然希望在下一版本中使用它。
if record from pricing_groups is deleted that is attached to a record from user_groups, it's ok to show the user group info. 如果删除了来自pricing_groups的记录,该记录附加到user_groups的记录,则可以显示用户组信息。 in fact, i want it to display the user group info. 事实上,我希望它显示用户组信息。 but if it's the other way around...if a user group is deleted, then it's ok not to show either the user group info or pricing group info. 但如果是相反的方式......如果用户组被删除,则可以不显示用户组信息或定价组信息。
To include all records from user_groups
you would need to use a LEFT JOIN
like so: 要包含user_groups
所有记录,您需要使用LEFT JOIN
如下所示:
SELECT ug.*, pg.*
FROM user_groups AS ug
LEFT JOIN pricing_groups AS pg
ON ug.pricing_group_id = pg.id
LIMIT 2,18446744073709551615
The LEFT JOIN
returns all rows from the left table ( user_groups
), with the matching rows from the right table ( pricing_groups
). LEFT JOIN
返回左表( user_groups
)中的所有行,右表中的匹配行( pricing_groups
)。 The result is NULL in the right side when there is no match. 没有匹配时,右侧的结果为NULL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.