我有兩個表格,舊格式和新格式的用戶。我想將舊格式的用戶與單獨的表格進行匹配,然後排除也顯示在新用戶格式表格中的所有用戶。我的數據是這樣的:選擇MYSQL結果,其中表1和2匹配,2和3不匹配
Table newUsers:
+----+-------+-------+----------+
| id | oldid | first | last |
+----+-------+-------+----------+
| 1 | 10 | John | Kennedy |
| 2 | 66 | Mitch | Kupchak |
+----+-------+-------+----------+
Table posts:
+----+---------+
| id | user_id |
+----+---------+
| 1 | 10 |
| 1 | 66 |
| 1 | 88 |
| 2 | 88 |
| 2 | 28 |
| 3 | 10 |
+----+---------+
Table oldUsers:
+----+----------+-------+----------+
| id | username | first | last |
+----+----------+-------+----------+
| 10 | A | John | Kennedy |
| 66 | B | Mitch | Kupchak |
| 88 | C | Dale | Earnhardt|
+----+----------+-------+----------+
Result wantend:
+----+----------+-------+----------+
| id | username | first | last |
+----+----------+-------+----------+
| 88 | C | Dale | Earnhardt|
+----+----------+-------+----------+
我想通過指定選擇我的結果:posts.id = 1和posts.user_id = oldUsers.id和newUsers.oldid = oldUsers.id所以我只收到oldUser .id等於88,因爲他不在新用戶列表中。
我試過各種JOINS和SUBQUERIES。我不斷收到newUsers表中的所有結果,而不是結果減去相應的條目。
這工作,但花了3.5秒,當做解釋說,它說創造了第一個左連接噸行。所以使用效率太低。 –
你需要索引到表中,我已經添加了表可能需要的索引。我假設來自olduser和newuser的id都是主鍵,因此不需要在它們上應用額外的索引。 –
添加了它們,它確實將它平均降到了0.023。但是我自己的代碼仍然執行速度提高了5.22倍。 –