2017-09-25 71 views
-6

一个记录我有这个疑问,它连接两个表,并给我所有的数据结果一个一个条件CATID获得基于catname

“录像”

SELECT 
pm_categories_images.Image, 
pm_categories_images.FileURL, 
pm_categories.catname, 
pm_categories.`status`, 
pm_categories.sortorder, 
pm_categories.parentID, 
pm_categories_images.CatID 
FROM 
pm_categories 
LEFT JOIN pm_categories_images ON pm_categories_images.CatID = pm_categories.catID 
where pm_categories_images.CatID IN (select catid from pm_categories where 
parentID = (select catID from pm_categories where catname = 'Videography')) 

现在这部录像带有这样的结果

http://prntscr.com/gpkuyl

现在我想1记录每catname

+0

任何特定的 “记录” 或只是 “纪录”? (就像最近的那个,parentID最高的那个,排序次序最低的那个?什么?mySQL扩展了这个组,所以你可以用catName来分组,但我不认为这真的是你想要的第一个 – xQbert

+0

第一个其中一个就足够了 – reev

+0

数据库表没有顺序;你必须设置一个,所以你首先指的是排序顺序最低的顺序?另外where where子句将左连接否定为只有图像pm_Category将被显示最后,什么是PM_Categories的主键? – xQbert

回答

0

没有一个MCVE和实际需求上的图像从图像表和更好的理解想为什么你需要一个左您的where子句使得当加入它表现得像一个内在......以及为什么where子句如此复杂......我真的不确定问题出在什么后面......这是一个镜头......和一个DEMO:http://rextester.com/CRBN50943

样本数据的预期结果总是好的:我做了我自己的和几个假设

我把这个问题描述为:我想要一个类别列表,以及每个类别具有最早字母值的图像。

SELECT 
CI.Image, 
CI.FileURL, 
C.catname, 
C.`status`, 
C.sortorder, 
C.parentID, 
CI.CatID 
FROM pm_categories C 
INNER JOIN pm_categories_images CI 
    ON CI.CatID = C.catID 
INNER JOIN (SELECT Min(Image) MI, catID FROM pm_categories_images group by CATID) Z 
on CI.Image = Z.MI 
and CI.CatID = Z.CatId 
##WHERE C.catname = 'Videography' 
Order by sortOrder 

给予我们

+----+------------+-----------------------------------------------+-------------+--------+-----------+----------+-------+ 
| | Image |     FileURL     | catname | status | sortorder | parentID | CatID | 
+----+------------+-----------------------------------------------+-------------+--------+-----------+----------+-------+ 
| 1 | guid1.jpg | https://drive.google.com/BusinessID/Postings/ | Real Estate |  1 |   1 | NULL  |  1 | 
| 2 | guid4.jpg | https://drive.google.com/BusinessID/Postings/ | commercial |  1 |   2 | NULL  |  2 | 
| 3 | guid6.jpg | https://drive.google.com/BusinessID/Postings/ | Videography |  1 |   3 | NULL  |  3 | 
| 4 | guid10.jpg | https://drive.google.com/BusinessID/Postings/ | Other  |  1 |   4 | NULL  |  4 | 
| 5 | guid11.jpg | https://drive.google.com/BusinessID/Postings/ | LackingMCVE |  1 |   5 | NULL  |  5 | 
+----+------------+-----------------------------------------------+-------------+--------+-----------+----------+-------+