mysqlleft join去掉后面表的id
时间: 2023-11-09 12:51:23 浏览: 88
好的,我了解了您的要求。针对您的问题,可以通过在LEFT JOIN语句中省略后面表的id列来去掉该列的显示。具体的SQL语句如下所示:
SELECT t1.*, t2.some_column
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.foreign_key;
在这个例子中,使用LEFT JOIN连接两个表,但是只显示其中的一列(some_column),而不显示后面表(table2)的id列。
相关问题
mysql left join后去掉重复
在MySQL中,如果你想要执行一个LEFT JOIN并去除结果中的重复行,通常你需要使用UNION ALL或者DISTINCT关键字来实现。不过,LEFT JOIN本身并不直接阻止重复行,它只是根据关联条件返回左侧表的所有记录以及右侧表匹配的记录。如果你想要去除结果集中的重复记录,可以在执行完JOIN操作后,再对结果进行去重处理。
例如,假设你有两个表,table1和table2,你想按照某个字段(如id)做LEFT JOIN,并去除重复项,你可以这样做:
```sql
SELECT table1.*, table2.*
FROM table1
LEFT JOIN table2 ON table1.id = table2.id
GROUP BY table1.id, table2.id;
```
或者,如果你不想保留NULL值,可以先使用UNION ALL来合并两个表,然后使用DISTINCT去重:
```sql
(SELECT table1.*, NULL AS table2_column
FROM table1
UNION ALL
SELECT table2.*, table2_column
FROM table2)
DISTINCT
ON (table1.id, table2_column);
```
这里假设table2_column是table2中的一个列名,你可以根据实际情况替换。
SELECT a.comm_id, c.Title AS house_keeper_name, ( SELECT build.id FROM erp_base.tb_base_masterdata_resource AS resource INNER JOIN erp_base.tb_base_masterdata_resource AS unit ON unit.id = resource.parent_id INNER JOIN erp_base.tb_base_masterdata_resource AS build ON build.id = unit.parent_id WHERE resource.id = a.resource_id ) AS resource_group, b.resource_type FROM tb_charge_receipts_detail a LEFT JOIN erp_base.tb_base_masterdata_resource b ON a.resource_id = b.id LEFT JOIN erp_base.rf_dictionary c ON b.house_keeper = c.id GROUP BY a.comm_id, c.Title, resource_group, b.resource_type 在mysql中报SELECT list is not in GROUP BY clause and contains nonaggregated column 'erp_charge.a.resource_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by 如何做sql的修改
根据错误提示,可以尝试修改sql_mode,去掉only_full_group_by,或者添加a.resource_id到GROUP BY子句中,但是这样可能会导致查询结果不准确。为了避免这种情况,可以使用聚合函数来替代a.resource_id,比如使用MAX(a.resource_id)。
修改后的SQL语句如下:
SELECT a.comm_id, c.Title AS house_keeper_name,
(SELECT build.id FROM erp_base.tb_base_masterdata_resource AS resource
INNER JOIN erp_base.tb_base_masterdata_resource AS unit ON unit.id = resource.parent_id
INNER JOIN erp_base.tb_base_masterdata_resource AS build ON build.id = unit.parent_id
WHERE resource.id = a.resource_id) AS resource_group,
b.resource_type
FROM tb_charge_receipts_detail a
LEFT JOIN erp_base.tb_base_masterdata_resource b ON a.resource_id = b.id
LEFT JOIN erp_base.rf_dictionary c ON b.house_keeper = c.id
GROUP BY a.comm_id, c.Title, resource_group, b.resource_type
阅读全文