select p3.vehicle_code, p3.harness_name, p3.harness_number, p2.harness_connect_code, p2.connector_brand_name, p2.element_brand_name, p4.item_code, p4.k1_e_status, DISTINCT p2.element_brand_name,p2.connector_code from pz_k1_e12_item p1 left join pz_k1_e07_item p2 on p1.e07_id = p2.id left join pz_k1_e07 p3 on p2.harness_id = p3.id left join pz_k1_e02 p4 on p4.id = p1.e02_id where p3.vehicle_code='SX11AD-2021' GROUP BY p2.element_brand_name,p2.connector_code > 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT p2.element_brand_name,p2.connector_code from pz_k1_e12_item p1 left j' at line 10 > 时间: 0.005s
时间: 2023-12-03 18:04:23 浏览: 91
根据你提供的 SQL 语句,错误信息显示在第 10 行,语法有误,需要修改。查询中不能在 SELECT 主语句中既使用 DISTINCT 关键字又使用其他列名。如果你想查询不重复的 element_brand_name 和 connector_code,可以将它们放到 GROUP BY 子句中,如下所示:
```
SELECT p3.vehicle_code, p3.harness_name, p3.harness_number, p2.harness_connect_code, p2.connector_brand_name, p2.element_brand_name, p4.item_code, p4.k1_e_status, p2.connector_code
FROM pz_k1_e12_item p1
LEFT JOIN pz_k1_e07_item p2 ON p1.e07_id = p2.id
LEFT JOIN pz_k1_e07 p3 ON p2.harness_id = p3.id
LEFT JOIN pz_k1_e02 p4 ON p4.id = p1.e02_id
WHERE p3.vehicle_code='SX11AD-2021'
GROUP BY p2.element_brand_name, p2.connector_code;
```
这样就可以查询到不重复的 element_brand_name 和 connector_code 列,并且不会有语法错误了。
相关问题
select p2.element_brand_name, p2.connector_code, p3.vehicle_codes from pz_k1_e12_item p1 left join pz_k1_e07_item p2 on p1.e07_id = p2.id left join pz_k1_e07 p3 on p2.harness_id = p3.id left join pz_k1_e02 p4 on p4.id = p1.e02_id where p3.vehicle_code='SX11AD-2021' GROUP BY p2.element_brand_name,p2.connector_code > 1054 - Unknown column 'p3.vehicle_codes' in 'field list' > 时间: 0.002s
根据你提供的 SQL 语句,错误信息显示在第 1 行,在 SELECT 子句中查询了一个不存在的列 p3.vehicle_codes,需要修改。如果你想查询 p3 表中的 vehicle_code 列,可以将其加入 SELECT 子句中,如下所示:
```
SELECT p2.element_brand_name, p2.connector_code, p3.vehicle_code
FROM pz_k1_e12_item p1
LEFT JOIN pz_k1_e07_item p2 ON p1.e07_id = p2.id
LEFT JOIN pz_k1_e07 p3 ON p2.harness_id = p3.id
LEFT JOIN pz_k1_e02 p4 ON p4.id = p1.e02_id
WHERE p3.vehicle_code='SX11AD-2021'
GROUP BY p2.element_brand_name,p2.connector_code;
```
这样就可以查询到 p3 表中的 vehicle_code 列,并且不会有未知列的错误了。
p2.connector_brand_name, p2.element_brand_name, p4.item_code, p4.k1_e_status from pz_k1_e12_item p1 left join pz_k1_e07_item p2 on p1.e07_id = p2.id left join pz_k1_e07 p3 on p2.harness_id = p3.id left join pz_k1_e02 p4 on p4.id = p1.e02_id where p3.vehicle_code='SX11AD-2021' GROUP BY p2.element_brand_name,p2.connector_code > 1055 - Expression #4 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'edds.p3.harness_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by > 时间: 0.005s
根据你提供的 SQL 语句,错误信息显示在第 1 行,在 SELECT 子句中查询了一个未加入 GROUP BY 子句的列 p3.harness_name,需要修改。此外,你的 MySQL 数据库设置了 sql_mode=only_full_group_by,表示只有在 SELECT 语句中的列名都出现在 GROUP BY 子句中时,才能执行查询,因此还需要修改查询语句。如果你想查询 p2.connector_brand_name、p2.element_brand_name、p4.item_code 和 p4.k1_e_status 列,可以将它们加入 SELECT 子句和 GROUP BY 子句中,如下所示:
```
SELECT p2.connector_brand_name, p2.element_brand_name, p4.item_code, p4.k1_e_status
FROM pz_k1_e12_item p1
LEFT JOIN pz_k1_e07_item p2 ON p1.e07_id = p2.id
LEFT JOIN pz_k1_e07 p3 ON p2.harness_id = p3.id
LEFT JOIN pz_k1_e02 p4 ON p4.id = p1.e02_id
WHERE p3.vehicle_code='SX11AD-2021'
GROUP BY p2.connector_brand_name, p2.element_brand_name, p4.item_code, p4.k1_e_status;
```
这样就可以查询到 p2.connector_brand_name、p2.element_brand_name、p4.item_code 和 p4.k1_e_status 列,并且不会有 GROUP BY 子句的错误了。
阅读全文