mysql - 合并两个选择查询。让它们独一无二

标签 mysql sql join merge

有人可以提示我如何合并这两个 mysql 查询吗?

正如您所看到的,distinct 也存在一个错误。我需要不重复的 topicid 以及该 topicid 所在帖子的日期。

我添加了distinct只是为了让您了解我需要什么。获取ids后,出现第二个查询,读取topicdetails...

我被困在这个问题上。

从某个类别的最后 n,15 个帖子中获取唯一的 topicid,但带有日期:

SELECT distinct( p.topicid ), p.date as lastaction
FROM posts p 
WHERE p.category = ? 
ORDER BY p.id 
DESC 
LIMIT 
n, 15



SELECT t.id, t.title, t.date, t.state 
FROM topics t 
WHERE t.id in( $var_ids ) 

谢谢。

最佳答案

SELECT DISTINCT 

p.[topicid],
p.[date] AS lastaction,
t.[id],
t.[title],
t.[date],
t.[state]

FROM posts p 

INNER JOIN topics t
ON p.topicid = t.id


WHERE p.category = /* Enter Category */ AND t.id in ( $var_ids )
ORDER BY p.topicid DESC

用它作为 10 的开始。我不确定 'n' 是什么(例如 Column?),所以我把它省略了

关于mysql - 合并两个选择查询。让它们独一无二,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22978958/

相关文章:

mysql - 远程访问MySQL服务器

php - 为什么 while 循环显示单行?

SQL 多对多查询

python - 使用条件在 python pandas 中进行内部连接

mysql - 在 MySQL 表中查找并替换转义引号

mysql - 将tmp表中的特定列值更新(复制)到Mysql中的主表

mysql - 参数化 SQL 查询

java - Oracle 序列可以为空吗?

sql - 慢 SQL 查询 : using the same table in two different joins causes query to become 10x slower!

php - Laravel 在学校活跃时对用户进行身份验证