Mysql版本5.1。 代码示例:
mysql_query("SELECT * FROM abonents WHERE `session_id`='$current_sess_id' UNION SELECT * FROM abonents_problem WHERE `session_id`='$current_sess_id' UNION SELECT * FROM abonents_stop WHERE `session_id`='$current_sess_id'");
如果删除“UNION”则运行,例如:
mysql_query("SELECT * FROM abonents WHERE `session_id`='$current_sess_id'" );
“WTF代码”永远=):
$rez_11_one = mysql_query(
"SELECT * FROM abonents WHERE `session_id`='$current_sess_id'"
);
$rez_11_two = mysql_query(
"SELECT * FROM abonents_problem WHERE `session_id`='$current_sess_id'"
);
$rez_11_three = mysql_query(
"SELECT * FROM abonents_stop WHERE `session_id`='$current_sess_id'"
);
if ($rez_11_one) {
$rez_11 = $rez_11_one;
} elseif ($rez_11_two) {
$rez_11 = $rez_11_two;
} else {
$rez_11 = $rez_11_three;
}
感谢您的帮助
答案 0 :(得分:4)
对mysql文档UNION
的引用仅在所有查询返回相同数量的columns
时才有效。
http://dev.mysql.com/doc/refman/4.1/en/union.html
因此,当您使用选择器星号*
时,我认为这些表具有不同的列数。使用select
例如
SELECT 1,2 FROM foo UNION SELECT 1,2 FROM bar
为每个查询提供相同数量的列。
答案 1 :(得分:0)
尝试不使用" *" 喜欢这个
mysql_query("SELECT <table_column_name> FROM abonents WHERE `session_id`='$current_sess_id' UNION SELECT <table_column_name> FROM abonents_problem WHERE `session_id`='$current_sess_id' UNION SELECT <table_column_name> FROM abonents_stop WHERE `session_id`='$current_sess_id'");
答案 2 :(得分:0)
您可能需要在字符串中添加转义字符才能生成它:
mysql_query("SELECT * FROM abonents WHERE \`session_id\`=\'$current_sess_id\' UNION SELECT * FROM abonents_problem WHERE \`session_id\`=\'$current_sess_id\' UNION SELECT * FROM abonents_stop WHERE \`session_id\`=\'$current_sess_id\'");
答案 3 :(得分:0)
确保查询返回相同数量的列。根据UNION
的文档。它仅在查询返回相同列数时才有效