1. 程式人生 > >MySQL筆記7

MySQL筆記7

rom eight 表格 des font arm span zip let

1. Inner Joints 表示鏈接兩個表格共有部分。(Inner Joints)

當兩個表格的列名相同時,為避免歧義,需指定從哪個表格進行引用的說明。

%%sql 
SELECT d.dog_guid AS DogID, d.user_guid AS  UserID, AVG(r.rating) AS AvgRating,
        COUNT(r.rating) AS NumRatings, d.breed, d.breed_group, d.breed_type
FROM dogs d, reviews r
WHERE d.dog_guid=r.dog_guid AND
d.user_guid=r.user_guid GROUP BY UserID, DogID, d.breed, d.breed_group, d.breed_type HAVING NumRatings >= 10 ORDER BY AvgRating DESC LIMIT 200;

2.從多個表進行結合。

SELECT c.user_guid AS UserID, u.state, u.zip, d.dog_guid AS DogID, d.breed, d.breed_type, d.breed_group
FROM dogs d, complete_tests c, users u
WHERE d.dog_guid=c.dog_guid AND c.user_guid=u.user_guid AND c.test_name="Yawn Warm-up";

MySQL筆記7