问题
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