我有兩個表。UNION在MySQL中的問題
rp_format
+-----+--+--------------+
| fid | | recordformat |
+-----+--+--------------+
| 1 | | CD |
| 2 | | Vinyl |
| 3 | | DVD |
+-----+--+--------------+
rp_records
+----+--+--------+
| id | | format |
+----+--+--------+
| 1 | | 1 |
| 2 | | 2 |
| 3 | | 3 |
+----+--+--------+
我想達成什麼是從 「rp_format」 顯示一切。但我也想檢查一下,看看是否有「格式」中找到的「fid」值。
實例應顯示的頁面上這樣的:
fid recordformat
1 CD Remove this format
2 Vinyl Remove this format
3 DVD Remove this format
但是,假設一個「FID」的值在「格式」找到,那麼我想它要顯示這樣的頁面:
fid recordformat
1 CD Remove this format
2 Vinyl Can't remove this format
3 DVD Remove this format
「刪除此格式/無法刪除此格式」是將通過使用PHP檢查「fid」=「format」來顯示的文本。
這是到目前爲止我的SQL查詢:
global $wpdb;
$rpdb = $wpdb->prefix . 'rp_format';
$rpdb2 = $wpdb->prefix . 'rp_records';
$sql = "
SELECT *
FROM $rpdb
LEFT OUTER JOIN $rpdb2 ON $rpdb.fid = $rpdb2.format
UNION
SELECT *
FROM $rpdb
RIGHT OUTER JOIN $rpdb2 ON $rpdb.fid = $rpdb2.format
WHERE $rpdb.fid IS NOT NULL
";
我有此查詢的問題是,當「FID」中的「格式」被找到(比方說,它的發現10次)每這些10價值也將被輸出。
這怎麼解決?
親切的問候 約翰
你能與完整的表更新你的問題嗎?這對於回答很重要。你可以使用[this](http://ozh.github.io/ascii-tables/) – marijnz0r
Better @ marijnz0r? :) –
不清楚,如果在'rp_records.format'上找到'rp_format.fid',那麼它應該顯示'不能刪除這種格式',否則'刪除這種格式'這就是你想要實現的嗎? –