简体   繁体   English

我应该在这个MYSQL查询中使用JOIN或UNION吗?

[英]Should I use JOIN or UNION for this MYSQL query?

I am trying to display data from two different tables ordered by the time the data was created. 我试图显示在创建数据时排序的两个不同表中的数据。

Here are the 2 tables I am pulling data from: 以下是我从中提取数据的2个表:

active_projects active_projects

+------------+------------+---------+-------+-------------+
| project_id | ts_created | user_id | title | description |
+------------+------------+---------+-------+-------------+

likes 喜欢

+---------+------------+------------+
| user_id | project_id | ts_created |
+---------+------------+------------+

The desired result is all of the data retrieved from these columns displayed properly, ordered by this column: 所需的结果是从这些列中检索到的所有数据都正确显示,按此列排序:

+------------+
| ts_created |
+------------+

This is the query that I am currently using: 这是我目前使用的查询:

$sql = "SELECT *
        FROM (SELECT project_id AS ppid, 
                     ts_created AS pts,
                     user_id AS puid,
                     title AS ptitle,
                     description AS pdesc
              FROM active_projects 
              WHERE user_id = 12)
        UNION
             (SELECT id AS lid, 
                     user_id AS luid,
                     project_id AS lpid,
                     ts_created AS lts
              FROM likes 
              WHERE user_id = 12)";

But, it's not working the way I want to. 但是,它不按我想要的方式工作。 I've been trying to debug what's going on but right now it's just saying that it's not actually retrieving any of the data. 我一直在尝试调试正在发生的事情,但现在它只是说它实际上没有检索到任何数据。

I have been researching JOIN and UNION but am not sure which one I should be using for this query. 我一直在研究JOIN和UNION,但我不确定我应该使用哪一个查询。 Any assistance is greatly appreciated. 非常感谢任何帮助。

EDIT: The query is now displaying properly using @Samuel's answer. 编辑:现在使用@ Samuel的答案正确显示查询。 But, now the results are being displayed 3 times per row. 但是,现在结果每行显示3次。 I am doing this at the end of the query: 我在查询结束时这样做:

$act = mysql_query($sql);

Then, when I echo out the results I am using a while loop likes this: 然后,当我回应结果我使用while循环时喜欢这样:

while ($t = mysql_fetch_row($act)) {
    echo $t[1];
}

Any ideas as to why each row is being displayed 3 times? 有关为什么每行显示3次的任何想法?

Your query is quite awful in terms of runtime performance and it does not produce the result you wish. 您的查询在运行时性能方面非常糟糕,并且不会产生您希望的结果。 Probably more useful: 可能更有用:

$sql = "
    SELECT * FROM active_projects
    LEFT JOIN likes ON (active_projects.user_id = likes.user_id)
    WHERE user_id = 12";

Untested, but something like that is what you need. 未经测试,但类似的东西是你需要的。 This should create a result of all entries from active_project for the user_id 12 left joined with the table likes. 这应该创建一个来自active_project的所有条目的结果,其中user_id 12与表喜欢一起加入。

First your query has a problem. 首先你的查询有问题。 You can't do select * from (select ...) . 你不能select * from (select ...) there should be 'where' clause before calling another sub query. 在调用另一个子查询之前应该有'where'子句。

You should use join instead of union as union is used to show results for tables with the same column names/types (aliases) while join uses a foreign key from the other table to show a 'joined' output. 您应该使用join而不是union,因为union用于显示具有相同列名/类型(别名)的表的结果,而join使用另一个表中的外键来显示“已连接”输出。

Do the following: 请执行下列操作:

SELECT A.project_id AS ppid, 
       A.ts_created AS pts,
       A.user_id AS puid,
       A.title AS ptitle,
       A.description AS pdesc,
       B.id AS lid, 
       B.user_id AS luid,
       B.project_id AS lpid,
       B.ts_created AS lts
FROM active_projects A
LEFT JOIN likes B ON
       A.user_id = B.user_id
WHERE A.user_id = 12 ORDER BY B.ts_created;

Actually, you can choose between, RIGHT JOIN and LEFT JOIN. 实际上,您可以选择RIGHT JOIN和LEFT JOIN。

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

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