Mysql根据字段获取三组行

时间:2013-06-06 12:38:07

标签: php mysql

我想在查询中设置三组行,每行一行。每组都有不同的数据值。像这样:

$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());

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

感谢。

5 个答案:

答案 0 :(得分:3)

使用UNION

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

答案 1 :(得分:2)

使用union

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

    and 
    mysql_query($final_query);

答案 2 :(得分:2)

尝试Union Like

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

如果您还想允许重复,请尝试{/ 1}}

UNION ALL

答案 3 :(得分: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)

答案 4 :(得分: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)
");

更多选项here