MySQL - 使用另一个表中的列排序行,不要重复数据

时间:2016-04-16 08:24:36

标签: mysql join distinct repeat

  • 更新 - 没关系,我用PHP端修复它并使用" distinct"最终的查询(解决方案)是这样的:

    SELECT Distinct m.idDenuncia FROM denunciaMedicamentos AS m JOIN denuncias AS p WHERE p.id = m.idDenuncia ORDER BY m.medicamento

SELECT * FROM denunciaMedicamentos AS m
JOIN denuncias AS p
WHERE p.id=m.idDenuncia
ORDER BY m.medicamento

它有效,但它会重复列ididDenuncia。这是结果:

result

idDenuncia是对denuncias表的引用。

我想知道,我该怎么做才能重复数据(id)?

2 个答案:

答案 0 :(得分:1)

从一个表中选择所有表并从另一个表中选择将解决您的问题

SELECT  m.*,p.col1,p.col2 FROM denunciaMedicamentos AS m JOIN denuncias AS p WHERE p.id=m.idDenuncia  ORDER BY m.medicamento

答案 1 :(得分:0)

现在,我明白了,你不希望128在你的结果中重复。如果这是您想要使用的:

SELECT distinct(m.idDenuncia) FROM denunciaMedicamentos AS m JOIN denuncias AS p WHERE p.id=m.idDenuncia ORDER BY m.medicamento