1

我的查询中需要三组行,每组 100 行。每组具有不同的数据值。所以像这样:

$result = mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100") or die(mysql_error());
$result .= mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100") or die(mysql_error());
$result .= mysql_query("SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100") or die(mysql_error());

如何在一个查询中获得结果?

谢谢。

4

5 回答 5

3

使用UNION

SELECT * FROM tableA UNION SELECT * FROM tableB UNION SELECT * FROM tableC
于 2013-06-06T12:40:13.093 回答
2

利用union

    $final_query=$query1." union ".$query2." union ".$query3;

    and 
    mysql_query($final_query);
于 2013-06-06T12:40:34.013 回答
2

尝试联合喜欢

SELECT * FROM tableA UNION SELECT * FROM tableB UNION SELECT * FROM tableC

如果您也想允许重复,请UNION ALL尝试

SELECT * FROM tableA UNION ALL SELECT * FROM tableB UNION ALL SELECT * FROM tableC
于 2013-06-06T12:42:23.743 回答
1

您可以使用UNION来完成这项工作

(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100) 
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100)
UNION
(SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100)
于 2013-06-06T12:41:38.357 回答
1

一种方法是使用UNION.

$result = mysql_query("
  (SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='24' ORDER BY count DESC LIMIT 100)
  UNION
  (SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='23' ORDER BY count DESC LIMIT 100)
  UNION
  (SELECT * FROM actions WHERE appid='$appid' AND email!='' AND user_name!='' AND data='22' ORDER BY count DESC LIMIT 100)
");

更多选项在这里

于 2013-06-06T12:43:00.917 回答