简体   繁体   English

MySQL查询消除了此PHP代码的使用

[英]MySQL query to eliminate the use of this PHP code

I have one query that INNER JOIN s Table A with Table B and Table C, and I have another query that INNER JOIN s Table A with Table D. I could achieve what I want to do by merging the 2 results, removing duplicates and ordering them in PHP code, but I want to know if MySQL has this functionality, I also think it'd be faster and easier to code once I understand it. 我有一个查询,即INNER JOIN的表A与表B和表C,还有一个查询,即INNER JOIN的表A与表D。我可以通过合并两个结果,删除重复项和排序来实现我想做的事情它们用PHP代码编写,但是我想知道MySQL是否具有此功能,我还认为,一旦我理解了它,编写起来会更快,更容易。 Essentially, I want to have the results from Query 1 OR from Query 2. Perhaps the following will help: 本质上,我想从查询1或查询2获得结果。也许以下内容会有所帮助:

Query 1: 查询1:

SELECT pkAssociation, associations.strNameEn, associations.strNameFr
FROM associations
INNER JOIN eventassociations ON fkAssociation = pkAssociation
INNER JOIN events on fkEvent = pkEvent
WHERE events.enumDeleted = 'no'
GROUP BY pkAssociation

Query 2: 查询2:

SELECT pkAssociation, associations.strNameEn, associations.strNameFr
FROM associations
INNER JOIN associationprograms AS aprogs ON aprogs.fkAssociation = associations.pkAssociation
GROUP by pkAssociation

The tables don't have anything else of relevance that don't show up in the query. 这些表没有其他没有显示在查询中的相关内容。 I'm sorry if I'm not asking this correctly, I don't even know how to ask a question about this properly. 很抱歉,如果我没有正确地提出这个问题,我什至不知道如何正确地提出这个问题。 If the column names or sample data is needed, then I can provide some. 如果需要列名或样本数据,那么我可以提供一些。 Sorry for the inconvenience and long post. 不便之处,敬请原谅。

You want the UNION DISTINCT statement, placed between the two queries. 您希望将UNION DISTINCT语句放置在两个查询之间。 This will combine the result sets from both and remove duplicates. 这将合并两个结果集并删除重复项。 You can then place your ORDER BY clause after all UNIONS (if you have one). 然后,可以将ORDER BY子句放在所有UNIONS之后(如果有的话)。

For example: 例如:

SELECT col1, col2 FROM tableA

UNION DISTINCT

SELECT col1, col2 FROM tableB

ORDER BY col2 DESC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM