SQL DISTINCT子查询

时间:2017-06-10 10:12:58

标签: php mysql sql

是否可以将两个查询合并为一个?

非常感谢提前

$colname_Recordset_hot = "-1";
if (isset($_GET['Search'])) {
  $colname_Recordset_hot = $_GET['Search'];
}
$colnamef1_Recordset_hot = "%";
if (isset($_GET['f1'])) {
  $colnamef1_Recordset_hot = $_GET['f1'];
}
mysql_select_db($database_con_amazing, $con_amazing);
$query_Recordset_hot = sprintf("SELECT * FROM Hotel WHERE publish = 1 AND ad_country LIKE %s AND tag_1 LIKE %s OR ad_city LIKE %s OR category LIKE %s OR name LIKE %s OR tag_1 LIKE %s OR tag_2 LIKE %s OR tag_3 LIKE %s", GetSQLValueString("%" . $colname_Recordset_hot . "%", "text"),GetSQLValueString("%" . $colnamef1_Recordset_hot . "%", "text"),GetSQLValueString("%" . $colname_Recordset_hot . "%", "text"),GetSQLValueString("%" . $colname_Recordset_hot . "%", "text"),GetSQLValueString("%" . $colname_Recordset_hot . "%", "text"),GetSQLValueString("%" . $colname_Recordset_hot . "%", "text"),GetSQLValueString("%" . $colname_Recordset_hot . "%", "text"),GetSQLValueString("%" . $colname_Recordset_hot . "%", "text"));
$Recordset_hot = mysql_query($query_Recordset_hot, $con_amazing) or die(mysql_error());
$row_Recordset_hot = mysql_fetch_assoc($Recordset_hot);
$totalRows_Recordset_hot = mysql_num_rows($Recordset_hot);
mysql_select_db($database_con_amazing, $con_amazing);

我想在SELECT DISTINCT tag_1列上获得其他结果,但不希望两次运行相同的查询。

1 个答案:

答案 0 :(得分:0)

您应该能够使用UNION ALL组合查询:

SELECT CustomerName
FROM Customers
WHERE name LIKE %s
UNION ALL
SELECT DISTINCT address
FROM Customers
WHERE name LIKE %s;

我不确定它有什么优势。