Select Distinct Field on Join in MySQL

↘锁芯ラ 提交于 2019-12-25 04:47:04

问题


I am joining two large tables in MySQL based on a unique identifier they both share. Because there are a large number of fields, I do not want to list out all fields after SELECT. Instead I want to select all fields, but I do not want recurring fields (the shared unique identifier in this case) to be repeated.

With this example query:

SELECT *
FROM Gr3_PracMath_Jan11_D1 as a, student_list_011811 as b 
WHERE a.StudentID = b.StudentID

The field StudentID is repeated. Is there a way to prevent this?

Thank you for your help.


回答1:


I believe that if you do an explicit join with the USING keyword, you won't get a duplicate.

SELECT *
FROM Gr3_PracMath_Jan11_D1
LEFT JOIN student_list_011811 
USING (StudentID)



回答2:


I don't think there is. You might cut your work by listing only half the fields:

SELECT a.*, b.Field1, b.Field2...



回答3:


It is bad practice to not list out all of the columns, even if there are a lot of them. Just bite the bullet and write them out.



来源:https://stackoverflow.com/questions/4730044/select-distinct-field-on-join-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!