根据catname获取一条记录

时间:2017-09-25 18:13:52

标签: mysql

我有这个查询,它连接了两个表并给出了一个条件CATID下的所有数据的结果

  

'录像'

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条记录

1 个答案:

答案 0 :(得分:0)

没有MCVE和实际要求你想从图像表中得到哪个图像,并且当你的where子句使它表现得像内在时,更好地理解为什么你需要左连接...以及为什么在哪里条款是如此复杂......我真的不确定问题是什么......在这里拍摄......和一个演示: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 |
+----+------------+-----------------------------------------------+-------------+--------+-----------+----------+-------+