简体   繁体   English

Mysql / PHP / Smarty - 如何在一行中检索2组不同的行?

[英]Mysql/PHP/Smarty - How can I retrieve 2 different sets of rows in one row?

I've tried many different ways and searched through other questions and forums, I just can't find what I'm looking for (may be searching for wrong keywords). 我尝试了很多不同的方法,并通过其他问题和论坛搜索,我找不到我正在寻找的东西(可能是在寻找错误的关键词)。

Anyways, here's my structure: 无论如何,这是我的结构:

mycars 
--car_id (pk)
--make
--model
mycars_details
--car_id (pk)
--option1
--option2
mycars_gallery
--car_id (pk)
--imgcount
--imglink
--imgthumb

I need to pull all 3 tables, which works fine for mycars and mycars_details (there is only 1 row per PK), but my mycars_gallery has 3 rows(images) for 1 car. 我需要拉出所有3个表,这对于mycars和mycars_details(每个PK只有1行)工作正常,但我的mycars_gallery有1行汽车的3行(图像)。 User can upload up to 12 images for 1 car. 用户最多可以为1辆汽车上传12张图片。

So when I run this sql: 所以当我运行这个sql时:

SELECT * FROM mycars c 
LEFT OUTER JOIN mycars_gallery g 
ON c.car_id = g.car_id 
LEFT OUTER JOIN mycars_details d 
ON c.car_id = d.car_id 
WHERE c.status = 1 AND c.car_id = 27

Output: 输出:

|car_id|make|model|option1|option2|imgcount|imglink  |imgthumb   |
  --------------------------------------------------------------
|  27  |volk|jetta|   1   |   0   |   1    |link1.jpg|thumb1.jpeg|
|  27  |volk|jetta|   1   |   0   |   2    |link2.jpg|thumb2.jpeg|
|  27  |volk|jetta|   1   |   0   |   3    |link3.jpg|thumb3.jpeg|

So i get all these duplicates from other tables. 所以我从其他表中得到所有这些重复项。 This data I store in smarty array and I spit it out on the page using smarty loop. 这个数据存储在smarty数组中,我使用smarty循环将其吐出页面。

Something like this: 像这样的东西:

{section name=car loop=$SHOW_ARRAY}
    {$SHOW_ARRAY[car].make} - {$SHOW_ARRAY[car].model}<br>

    {$SHOW_ARRAY[car].imglink}
{/section}

Question: I feel like my query is wrong, how can I retrieve a single car with all of the images stored in database (mycars_gallery tbl) 问题:我觉得我的查询错了,如何检索存储在数据库中的所有图像的单个车辆(mycars_gallery tbl)

Try following query: 请尝试以下查询:

SELECT c.car_id,c.make,c.model, d.option1, d.option2,
MAX(CASE WHEN G.IMGTHUMB = 'thumb1.jpg' THEN G.IMGTHUMB ELSE 0 END) AS T1,
MAX(CASE WHEN G.IMGTHUMB = 'thumb2.jpg' THEN G.IMGTHUMB ELSE 0 END) AS T2,
MAX(CASE WHEN G.IMGTHUMB = 'thumb3.jpg' THEN G.IMGTHUMB ELSE 0 END) AS T3
FROM mycars c 
Inner JOIN mycars_gallery g 
ON c.car_id = g.car_id 
Inner JOIN mycars_details d 
ON c.car_id = d.car_id 
WHERE c.car_id = 27;

See Demo at: http://sqlfiddle.com/#!2/3e90b/13 请参阅演示: http//sqlfiddle.com/#!2/3e90b / 13

Also an possible alternative is the use of group_concat() function in mysql. 另外一种可能的替代方法是在mysql中使用group_concat()函数。

I am assuming that you are working on MySQL, Please use the following generalized query to achieve the same. 我假设您正在研究MySQL,请使用以下通用查询来实现相同的目标。

SET @sql = NULL;
SELECT 
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when g.imgthumb = ''',
      G.IMGTHUMB,
      ''' then g.imgthumb else 0 end) AS `',
      IMGTHUMB, '`'
    )
  ) INTO @sql
FROM  mycars c 
Inner JOIN mycars_gallery g 
ON c.car_id = g.car_id 
Inner JOIN mycars_details d 
ON c.car_id = d.car_id 
WHERE c.car_id = 27;

SET @sql = CONCAT('select c.car_id,c.make,c.model, d.option1, d.option2, ', @sql, ' 
                  from mycars c 
                  Inner JOIN mycars_gallery g 
                  ON c.car_id = g.car_id 
                  Inner JOIN mycars_details d 
                  ON c.car_id = d.car_id 
                  WHERE c.car_id = 27');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

You should be able to run as many images available in your data set. 您应该能够在数据集中运行尽可能多的图像。 A working fiddle can be found at: http://sqlfiddle.com/#!2/449638/25 可以在以下网址找到工作小提琴: http//sqlfiddle.com/#!2/449638/25

What Saurabh included in his comments about possibly using the group concat function would look like this: Saurabh在他关于可能使用group concat函数的评论中包含的内容如下所示:

SELECT c.*, 
  GROUP_CONCAT(g.imglink) AS links,
  GROUP_CONCAT(g.imgthumb) AS thumbs
FROM mycars c 
LEFT OUTER JOIN mycars_gallery g 
ON c.car_id = g.car_id 
LEFT OUTER JOIN mycars_details d 
ON c.car_id = d.car_id 
WHERE c.status = 1 AND c.car_id = 27
GROUP BY c.car_id;

This would give you the list of thumbs and links as single comma separated values. 这将为您提供拇指和链接列表作为单个逗号分隔值。

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

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