简体   繁体   English

将两个MySQL查询合并为一个

[英]Combining two MySQL queries into one

In my database, I have two tables: Replies and Threads. 在我的数据库中,我有两个表:Replies和Threads。

I'm trying to make a Contributions page showing what threads users have started or replied to. 我正在尝试创建一个“贡献”页面,以显示用户已启动或回复的线程。

Inside Replies are "ThreadId" and "Poster" which specify the Id of the thread it was replied to, and each row in Threads has an Id column as well as a "Poster" column. 内部答复是“ ThreadId”和“ Poster”,它们指定要答复的线程的ID,并且Threads中的每一行都有一个ID列和一个“ Poster”列。

I'm trying to make a query to get the rows of all THREADS in which a user has either posted or posted in. 我正在尝试进行查询,以获取用户已在其中张贴或在其中张贴的所有螺纹的行。

Example: 例:

$myUsername = "Bob";
$q = mysql_query("SELECT * FROM `Threads` WHERE `Poster`='$myUsername'
OR (another query to find if they had a row in Replies matching `ThreadId`='$tid'
AND `Poster`='$myUsername')");

Thanks 谢谢

You will need to use an IN clause to specify that the thread id should be in those returned by a query in the replies table. 您将需要使用IN子句来指定线程ID应该在答复表中的查询返回的那些ID中。 Something like this is I understand correctly the structure of your tables. 像这样的事情是我正确理解您的表的结构。

SELECT * FROM Threads WHERE Poster = $myUsername OR Id IN (
SELECT ThreadId FROM Replies WHERE Poster = $myUsername
)

Using Christina's solution probably won't result in a very efficient query. 使用克里斯蒂娜的解决方案可能不会导致非常有效的查询。 The following should be better: 以下应该更好:

SELECT * 
FROM Threads t1
WHERE t1.Poster='$myUsername'
UNION
SELECT *
FROM Threads t2
INNER JOIN replies r
ON t2.id=r.thread_id
WHERE t2.Poster<>'$myUsername'
AND r.Poster='$myUsername';

However: 然而:

1) do you really need all the columns from the threas table? 1)您真的需要威胁表中的所有列吗? Even if you do, using '*' is messy 即使您这样做,使用'*'也很麻烦

2) you are still going to get duplicates if the user made more more than one reply - but this can't be fixed while you are using '*' 2)如果用户做出的答复不止一个,您仍将获得重复-但这在使用'*'时无法解决

3) the query will still be slow eith large amounts of data if you don't have an index on poster in both tables. 3)如果您在两个表中的海报上都没有索引,则查询仍然很慢,无论是大量数据。

I have done this differently. 我做了不同的事情。 The two queries provided returned duplicates and what not, so I've decided to go a little longer way. 提供的两个查询返回了重复项,而没有返回,因此我决定走更长的路。

What I did: 我做了什么:

1) Go through each of the user's replies, and get the thread id 1)浏览用户的每个答复,并获得线程ID

2) Build a query string using these IDs, eg "OR Id=$threadId OR Id=$threadId2" etc 2)使用这些ID构建查询字符串,例如“ OR Id = $ threadId或Id = $ threadId2”等

3) Put the query string into a thread query such as SELECT * FROM Threads WHERE Poster=$user $queryString2 3)将查询字符串放入线程查询中,例如SELECT * FROM Threads WHERE Poster = $ user $ queryString2

4) Solution. 4)解决方案。 :) :)

Try this: 尝试这个:

You can use LEFT OUTER JOIN instead of IN operator 您可以使用LEFT OUTER JOIN代替IN运算符

SELECT T.* 
FROM Threads T 
LEFT OUTER JOIN Replies R ON T.Id = R.ThreadId AND R.Poster = $myUsername
WHERE T.Poster = $myUsername OR R.Id IS NOT NULL

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

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