當單獨運行以下每個SELECT(沒有UNION)時我正在按預期得到結果。使用UNION時我不會得到任何結果。
任何想法,爲什麼這不起作用?
$query = "
(SELECT * FROM projects WHERE public='1')
UNION
(SELECT * FROM projects JOIN project_region ON projects.id_project = project_region.id_project
JOIN user ON user.id_region = project_region.id_region WHERE user.user_id = {$current_user->ID})
UNION
(SELECT * FROM projects JOIN project_user ON projects.id_project = project_user.id_project
WHERE project_user.user_id = {$current_user->ID})
";
$projects = $wpdb->get_results($query);
if ($projects) {
foreach ($projects as $project) {
// output results
}
}
THX爲你的答覆,但問題是一個MySQL錯誤「列'id_project'字段列表中是不明確的」。請參閱下面的答案。 – joko13 2012-08-08 10:27:52
xto的答案是正確的答案。 joko13你的回答是xto的解釋的結果... – Nico 2014-02-07 14:56:43