简体   繁体   English

根据另一个SQL PHP的结果从一个表获取数据

[英]Getting data from one table based on results of another SQL PHP

I know this involves JOINS but I can't seem to find a working solution to what I'm trying to do. 我知道这涉及到JOINS,但是我似乎找不到我想要做的可行的解决方案。

I have 2 custom tables : 我有2个自定义表:

table1    |    table2
---------------------
id             id
uid            uid
track_id       track_id
date           date
art            active
info
blah
blah2

First I want to select everything WHERE uid =55 AND active =1 from table2 : 首先,我想从table2中选择所有uid = 55 AND active = 1的对象:

$tracks = $wpdb->get_results( "SELECT * FROM table2 WHERE uid = 55 AND active = 1");

And then match the track_id from table2 with results from table1 so I can traverse the table1 data. 然后将table2中的track_idtable1中的结果进行匹配,这样我就可以遍历table1数据。

I know I can do it like this : 我知道我可以这样做:

foreach( $tracks as $track ) {

$this_track = $track->track_id;

$results = $wpdb->get_results( "SELECT * FROM table1 WHERE track_id = $this_track");

// Do stuff here

}

But this is the part where it gets tricky... 但这是棘手的部分...

I then want to ORDER the $results from table1 by date DESC from table2 然后我要订购表1表2 日期 DESC的$结果

And this is where I'm lost... 这就是我迷路的地方...

Effectively I want (pseudo code) : 实际上我想要(伪代码):

$results = $wpdb->get_results( "SELECT * FROM table1 WHERE track_id = $this_track" ORDER BY date DESC FROM table2);

As well as that last bit, I know I can do this entire routine with JOINS to keep this all in one query and make it way more efficient but I just don't know how. 除了最后一点,我知道我可以使用JOINS来完成整个例程,以将所有内容保留在一个查询中,并使其效率更高,但我只是不知道如何。

So just to be clear, my overall routine should be like this : 因此,为了清楚起见,我的总体例程应如下所示:

Get all instances of track_id from table2 where user_id=55 and active=1, then use those results to match the track_id to every result in table1 with the same track_id and then sort the results by date back over from table2 从table2中获取user_id = 55且active = 1的track_id的所有实例,然后使用这些结果将track_id与table1中具有相同track_id的每个结果进行匹配,然后按日期对结果进行排序,从table2开始

Psuedo code, I know it contains nonsense : 伪代码,我知道它包含废话:

$finalresults = $wpdb->get_results( "SELECT * FROM table2 where uid=55 AND active=1 THEN SELECT * FROM table1 WHERE track_id = "the track_id from the first query" THEN ORDER BY date DESC FROM table2);

Try with this query 尝试使用此查询

SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1 ORDER BY t2.date DESC;

Edit : Explanation of what this query is doing. 编辑 :此查询正在做什么的说明。 and inverted the order of the tables retrieved in the query (this don't affect the final datatset, i did this to make to follow the logic of the explanation. 并反转查询中检索到的表的顺序(这不会影响最终数据集,我这样做是为了遵循解释的逻辑。

1.- Begin with retrieving all rows from table2 (theres is no specific reason because i used table2 over table1 , I'm only following an logical order), using the criteria that you specified iud=55 and active=1 1.-首先从table2检索所有行(没有特殊原因,因为我在table1使用了table2 ,我只是遵循逻辑顺序),请使用您指定的标准iud=55 and active=1

SELECT * FROM table2 WHERE uid=55 AND active=1;

2.- but as you said you need to expand the data retrieved in table2 with some information in table1 , that's exactly what it is the directive JOIN made, and we are using INNER JOIN because this type of JOIN will show rows ONLY if data for the uid=55 is present on table1, if there is NO data for the uid=55 present on both TABLES then mysql wil show empty the recordset (0 Rows selected). 2.-但是正如您所说,您需要使用table1中的一些信息来扩展在table2检索到的数据,这正是JOIN指令所做的,并且我们正在使用INNER JOIN,因为这种类型的JOIN仅在以下情况下显示行: uid = 55存在于table1上,如果两个表上都没有uid = 55的数据,则mysql将显示记录集为空(选择0行)。

in the ON(...) part I specify which criteria mysql will use to compara both tables for match in this case will compare that track_id on table2 it is the same that the specified on table1, if this codition is met then mysql considers it as a match. ON(...)部分中,我指定mysql将比较两个表以进行匹配的条件,在这种情况下,将比较table2上的track_id与table1上指定的相同,如果满足此条件,则mysql认为作为比赛。

anly for convenience and because i'm adding a Second table i gave an Alias to each one t1 and t2. 为方便起见,并且因为我要添加第二张表,所以我给每个t1和t2一个别名。

then the query now seems like this 然后查询现在看起来像这样

SELECT * FROM table2 AS t2 INNER JOIN table1 AS t1 ON(t1.track.id = t2.track_id) WHERE t2.uid=55 AND t2.active=1;

3.- but then raise a problem, both tables has rows with the same field names, and this is something that DBMS don't like in their queries, to avoid this situation in the query i only show the fields (id, uid and track_id) from one table in this case t1 (t1.*) and only show the fields that doesn't have this problem from t2 (t2.date AS t2date, t2.active). 3.-但随后出现一个问题,两个表都具有相同字段名的行,这是DBMS在查询中不喜欢的,为避免这种情况,我只显示字段(id,uid和在这种情况下是t1(t1。*)的一个表中的track_id),并且仅显示从t2起没有此问题的字段(t2.date AS t2date,t2.active)。 in this way mysql won't throw any error. 这样,mysql不会抛出任何错误。

SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1;

4.- for the final step i specify to mysql that i want all found rows ordered descent by a field in the table2; 4.-对于最后一步,我向mysql指定我希望所有找到的行都由table2中的字段按顺序下降;

ORDER BY t2.date DESC; 

then this criteria will be applied to the whole selected rows. 那么此标准将应用于整个选定的行。 and the final query has this form. 最后的查询具有这种形式。

SELECT t1.* ,t2.date AS t2date, t2.active FROM table2 AS t2 INNER JOIN table1 AS t1 ON (t1.track_id = t2.track_id) WHERE t2.uid=55 AND t2.active=1 ORDER BY t2.date DESC;

if is not completely clear you can ask ... 如果不是很清楚,你可以问...

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

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