我有两个mysql查询:
$sql = "SELECT * FROM content WHERE threadName LIKE '%$filter%' ORDER BY lastUpdated desc";和
$sql = "SELECT * FROM content ORDER BY lastUpdated desc";最终结果是从一个特定表“content”返回所有行,但将匹配变量$filter的行放在顶部。有没有一个查询可以组合这两个查询,或者我应该使用一个连接?
............still不走运。这是我目前所拥有的:
$sql = "SELECT * FROM content WHERE threadName LIKE '%$filter%' ORDER BY lastUpdated desc UNION SELECT * FROM content WHERE threadName NOT LIKE '%$filter%' ORDER BY lastUpdated desc";这就给出了:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/andrew/public_html/PHP/threadDisplay.php on line 20尽管第一部分(在UNION之前)在单独使用时可以工作。
发布于 2010-03-13 06:23:03
使用CASE STATEMENT怎么样?
就像这样
SELECT *
FROM content
ORDER BY CASE
WHEN threadName LIKE '%$filter%' THEN 0
ELSE 1
END ASC,
lastUpdated desc发布于 2010-03-13 06:24:12
(SELECT *
FROM content
WHERE threadName LIKE '%$filter%')
UNION
(SELECT *
FROM content
WHERE threadName NOT LIKE '%$filter%')
ORDER BY lastUpdated desc编辑:MySQL需要一些括号,并且需要在联合之后执行订单,抱歉忘记了这一点。
https://stackoverflow.com/questions/2436274
复制相似问题