简体   繁体   English

如何使用join编写mysql查询,如果删除了一个表中的记录,仍会显示部分信息?

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

相关问题 如何在PHP-MySQL中联接两个表,该表与第二个表中的ID匹配并仍在表1中显示特定记录? - How to join two tables in PHP - MySQL that match an ID in the second table and still display specific record in table 1? Codeigniter Active Record / MySQL Join查询-如果不存在表行之一,如何返回结果 - Codeigniter Active Record / MySQL Join Query - How to Return Results if one of the Table Rows is Not Present mysql php android。 从表中删除了所有行,但最后一行仍显示在列表视图中 - mysql php android. Deleted all rows from a table yet the last one still shows up on listview 我想连接数据并显示一个表中的所有记录和另一个表中的一个记录 - I want to join data and display all records from one table and just one record from another table 从一个表查询后,从另一表获取更多信息并加入结果 - After query from one table get more info from other table and join result 如何在mysql中通过一个查询从两个不同的表中选择两个记录 - How to select two record from two different table with one query in mysql 如何使用联接仅从另一个表中获取一条记录 - How to get only one record from another table using Join 自定义联接查询以从3个表中获取记录 - custom join query to get record from 3 table 如何在php中显示一个到多个mysql表的记录? - How to display record from one to many mysql tables in php? MySQL表联接:一个记录,两个字段,另一表中的id - MySQL Table Join: One record, two fields, id in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM