[英]Is it possible to combine MySQL queries to multiple tables into a single query based on the results from one of the queries?
The $userid
of the currently logged in user is all the is currently available in the PHP code. 当前登录用户的
$userid
是PHP代码中当前可用的全部。 I want to run a query against the mysql tables to return all of the status updates for myself and for friends ordered by createddate DESC
. 我想对mysql表运行一个查询,以返回我自己和由
createddate DESC
订购的朋友的所有状态更新。
MySQL Sample database tables: MySQL示例数据库表:
[statusupdates] [状态更新]
statusupdateid int(8)
ownerid int(8)
message varchar(250)
createddate datetime
[friends] [网友]
friendid int(8)
requestfrom int(8)
requestto int(8)
daterequested datetime
dateupdated datetime
status varchar(1)
Question: Can I perform a single string query that returns each statusupdates.userid
and the statusupdates.message
ordered by statusupdates.createddate DESC
? 问:我可以执行一个查询字符串,返回各自
statusupdates.userid
和statusupdates.message
通过有序statusupdates.createddate DESC
?
Or do I have to run a query for each friends
record where the $userid
is in either the friends.requestfrom
or friends.requestto
then, run another query for alternate friends.requestfrom
or friends.requestto
(the one that doesn't include $userid
), then sort all of the results by statusupdate.createddate
and then get the statusupdates.message
? 或者我必须为每个
friends
记录运行查询,其中$userid
在friends.requestfrom
或friends.requestto
然后为备用friends.requestfrom
或friends.requestto
运行另一个查询(不包括$userid
那个) $userid
),然后按statusupdate.createddate
对所有结果进行statusupdate.createddate
,然后获取statusupdates.message
?
You want to look at MySQL Joins. 你想看看MySQL连接。
I think this may do something like what you're after, but it will almost definitely need debugging! 我认为这可能会像你所追求的那样,但几乎肯定需要调试!
SELECT DISTINCT s.ownerid, s.message
FROM statusupdates s
LEFT JOIN friends f ON ($userid = f.requestfrom)
LEFT JOIN friends f ON ($userid = f.requestto)
ORDER BY s.createddate;
This is untested, but should work or at least get you in the right direction. 这是未经测试的,但应该起作用或者至少让你朝着正确的方向前进。
You could use IN()
where you get a list of userid
s from a sub query. 您可以使用
IN()
从子查询中获取userid
列表。 That subquery does a UNION
on 2 queries - 1st to get the requestfrom
userid
s, and 2nd to get the requestto
userids
. 子查询做了
UNION
2个查询-第一次拿到requestfrom
userid
S,和第2拿到requestto
userids
。 Finally we add an OR
to include the current userid.
最后,我们添加一个
OR
来包含当前的userid.
also, I assume that you also want to filter out where status = 1
, as you don't want updates from those who have not confirmed friendships 另外,我假设您还想过滤掉
status = 1
,因为您不希望那些尚未确认友谊的人进行更新
SELECT s.ownerid, s.message
FROM statusupdates s
WHERE s.ownerid IN (
SELECT f1.requestfrom
FROM friends f1
WHERE f1.requestto = $userid
AND f1.status = 1
UNION
SELECT f2.requestto
FROM friends f2
WHERE f2.requestfrom = $userid
AND f2.status = 1
)
OR s.ownerid = $userid
ORDER BY s.createddate DESC
take a look at this sqlFiddle example - http://sqlfiddle.com/#!2/85ea0/3 看看这个sqlFiddle示例 - http://sqlfiddle.com/#!2/85ea0/3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.