0

我想要做。如果相同的 id 内容超过 1,则只需从 mysql 获取 1 项。我不知道该怎么做。在我的 MySQL 查询下方。

$myboxexi=mysql_query("select box.upload_type,box.id,box.box_type,box.page_name,box.title,box.connect,box.type,box.uid,box.description,box.image,box.url,box.status,box.date,box.time
from {$statement} where pages.uid='".$session_id."' or catsb.uid='".$session_id."' and box.status='".$approval."' order by box.id desc limit {$startpoint} , {$limit}");

问题是我通过(box.id)从另一个表中获取数据,并且某些表有 4 次相同的项目,而我 4 次得到 1 个项目。所以如果有任何物品超过 1 件,我想获得 1 件物品

有人知道吗?

4

1 回答 1

1

在查询结束时使用 LIMIT 1

此外,如果您获得 2 个或更多相等的行,您可以使用DISTINCTGROUP BY来获得唯一的行

不同的语法:

SELECT DISTINCT t1.field1, t1.field2, t1.field3
FROM table1 AS t1, table2 as t2
WHERE t1.field1=t2.field1
AND t1.field1=value1

GROUP BY 语法:

SELECT t1.field1, t1.field2, t1.field3
FROM table1 AS t1, table2 as t2
WHERE t1.field1=t2.field1
AND t1.field1=value1
GROUP BY t2.field2

在您的情况下,您可以使用不同的:

$query = "SELECT DISTINCT box.upload_type, box.id, box.box_type, box.page_name, ";
$query .= "box.title, box.connect, box.type, box.uid, box.description, ";
$query .= "box.image, box.url, box.status, box.date, box.time ";
$query .= "FROM {$statement} ";
$query .= "WHERE pages.uid='".$session_id."' ";
$query .= "OR catsb.uid='".$session_id."' ";
$query .= "AND box.status='".$approval."' ";
$query .= "ORDER BY box.id DESC ";
$query .= "LIMIT {$startpoint} , {$limit}";

$myboxexi=mysql_query($query);

更多关于DISTINCT你可以在这里找到:http: //dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html

更多关于GROUP BY你可以在这里找到:http: //dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

尝试编写格式化代码。您和其他程序员很容易调试。

于 2013-10-07T22:25:07.223 回答