简体   繁体   English

加入1个表中具有单个ID /行但在另一个表中链接到多个行(相同ID)的SQL查询?

[英]Join SQL query that has single id/row in 1 table, but links to multiple rows(same id) in another table?

First sorry for the long question title. 首先对不起,标题很长。

My question/situation is as such. 我的问题/情况就是这样。

1.) I have 2 tables in mysql 1.)我在mysql中有2个表

2.) In first table, each listing has a unique id(each listing is in 1 row) 2.)在第一个表格中,每个列表都有一个唯一的ID(每个列表在一行中)

3.) In the second table it has the name/tags for images linked to the listing id,from the first table 3.)在第二张表中,它具有链接到第一张表中列表ID的图像的名称/标记

4.) Each listing can have multiple images(multiple row in the second table). 4.)每个清单可以有多个图像(第二个表中的多行)。

What i am trying to do is to pull all the listings from table 1 and then use the listing.id from table one to pull all the rows of images from table 2 that are linked to the listing.id. 我想做的是从表1中提取所有清单,然后从表1中使用Listing.id从表2中提取链接到listing.id的所有图像行。 I am confused at the moment because there are multiple rows that has the same listing.id from table 2. ANd i tried query to display* but it only echo the last image(row) from table 2. 我现在很困惑,因为表2中有多行具有相同的listing.id。我尝试查询以显示*,但它只回显表2中的最后一个图像(行)。

It doesnt seem to work when i join the 2 tables. 当我加入2表时,它似乎不起作用。 And i am not sure if i query it twice then push array together. 而且我不确定是否查询两次然后将数组推在一起。

Thanks for your time 谢谢你的时间

 $result=mysqli_query($con,"SELECT * FROM Listing JOIN listingpic ON 
    (Listing.id = listingpic.listingid)

 WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY GROUP BY Listing.id ORDER BY Listing.id DESC") or die( mysqli_error($con));

while ($row = mysqli_fetch_assoc($result))
        {
  $output[] = $row;
        }
        if (!empty($output)){

        echo json_encode( $output );}
        else{

            echo json_encode( [] );
        }

You only get one result per id, because you used GROUP BY Listing.id . 每个id只能得到一个结果,因为您使用的是GROUP BY Listing.id If you do not group them, you get one result row for each table 2 row, including the Listing id each time. 如果不对它们进行分组,则每个表2行都会得到一个结果行,每次都包含列表ID。

If you wish to retrieve the data in one query in the form "one id: multiple data", you can use GROUP_CONCAT for example and then explode() the retrieved string result. 如果希望在一个查询中以“一个ID:多个数据”的形式检索数据,则可以使用GROUP_CONCAT例如,然后explode()检索到的字符串结果。

Otherwise get all ids from table 1 and then iterate over them in PHP and do one additional query per ID 否则,从表1中获取所有ID,然后在PHP中对其进行迭代,并对每个ID进行一次附加查询

Pro tip: Don't use the viciously confusing MySQL extension to GROUP BY . 专家提示:不要对MySQL GROUP BY使用令人困惑的MySQL扩展。 Read this: http://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html 阅读此: http : //dev.mysql.com/doc/refman/5.6/en/group-by-handling.html

Pro tip: Don't use SELECT * , especially when you're joining tables. 专家提示:不要使用SELECT * ,尤其是在连接表时。 Instead, enumerate the columns you want in your result set. 而是枚举结果集中所需的列。

Inherent to SQL is the idea that resultsets, like tables, are rectangular. SQL的固有思想是结果集(如表)是矩形的。 They have rows and columns. 他们有行和列。 Each row usually represents some real world item -- an "entity" -- and each column represents some attribute of that entity. 每行通常代表一个现实世界的项目-一个“实体”-每列代表该实体的某些属性。

The result set you describe will, inherently, repeat information from your first table so it can show the info from the second table row by row. 您描述的结果集本质上将重复第一个表中的信息,以便可以逐行显示第二个表中的信息。

What you want for a query is this, I think. 我想您要查询的是这个。

SELECT Listing.id, listing.date, 
       listingpic.id, listingpic.url, listingpic.caption
  FROM Listing
  JOIN listingpic ON  Listing.id = listingpic.listingid
  WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY 
  ORDER BY Listing.id DESC, listingpic.id

This will give you one row per image. 这将为您提供每张图像一行。

If you're running out of memory it's because your result set is massive. 如果内存不足,那是因为结果集很大。 You may want to limit it somehow, either using a first and last publication date: 您可能想使用第一个和最后一个发布日期以某种方式限制它:

  WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+300 DAY 
    AND date <  curdate() - INTERVAL DAYOFWEEK(curdate())+293 DAY

or with a LIMIT clause. 或带有LIMIT子句。

  ORDER BY Listing.id DESC, listingpic.id
  LIMIT 100

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

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