我有 3 个问题:
SELECT If(((cat_id = 9) or (cat_id = 5)),url_download,"") as Maps
FROM `b96e8_jdownloads_files`
where cat_id not in (7,8,4,0,12,10,11)
order by date_added DESC
SELECT If(((cat_id = 11) OR (cat_id = 10)),url_download,"") as Publications
FROM `b96e8_jdownloads_files`
where cat_id not in (7,8,4,9,5,12,0)
order by date_added DESC
select If(cat_id = 12,url_download,"") as Others
FROM `b96e8_jdownloads_files`
where cat_id not in (7,8,4,0,10,11,5,9)
order by date_added DESC
我想结合它们来实现这个输出。这样我就可以从表中获取数据并将其输出。
| Maps | Publications | Others |
--------------------------------------------------------------------------------------------
im_0709.zip | Annual Report 2009.rar | ccIlonggoVer.rar
namriaVacantPosition07 17 14b.rar | im_0710.zip | ccIlocanoVer.rar
namriaVacantPosition6 27 14.rar | im_1208.zip | ccFilipinoVer.rar
namriaVacantPosition5 15 14.rar | im_0807.zip | BRP Brochure.rar
| | brochureNamria.rar
| | brochureGTC.rar
| | Severe Wind.rar
| | Ground Shaking.rar
| | Flood.rar
但是,当我尝试使用此查询组合三个查询时
SELECT file_title, If(((cat_id = 9) or (cat_id = 5)),url_download,"") as Maps,
If(((cat_id = 11) OR (cat_id = 10)),url_download,"") as Publications,
If(cat_id = 12,url_download,"") as Others
FROM `b96e8_jdownloads_files`
where cat_id not in (7,8,4,0)
order by date_added DESC
我得到了这个输出
| Maps | Publications | Others |
--------------------------------------------------------------------------------------------
NULL | NULL | ccIlonggoVer.rar
NULL | NULL | ccIlocanoVer.rar
NULL | NULL | ccFilipinoVer.rar
NULL | NULL | BRP Brochure.rar
| | brochureNamria.rar
| | brochureGTC.rar
| | Severe Wind.rar
| | Ground Shaking.rar
| | Flood.rar
NULL | Annual Report 2009.rar | NULL
im_0709.zip | NULL | NULL
NULL | im_0710.zip | NULL
NULL | im_1208.zip | NULL
NULL | im_0807.zip | NULL
namriaVacantPosition07 17 14b.rar | NULL | NULL
namriaVacantPosition06 27 14.rar | NULL | NULL
namriaVacantPosition05 15 14.rar | NULL | NULL
最佳答案
您使用 SQL 创建记录的结果集。
在每条记录中,字段之间都有某种关系。
现在,在您想要的输出中,我敢猜测 namriaVacantPosition07 17 14b.rar
、im_0710.zip
和 ccIlocanoVer.rar
绝对有彼此之间没有关系,除了它们碰巧在同一个数据库中的某个地方,并且巧合碰巧出现在同一行上,如果你把三个不相关的列表放在一个旁边另一个。
如果没有逻辑上的理由来描述为什么这三件事应该出现在同一行,那么在 SQL 中就没有办法做到这一点。
事实上,您甚至不应该尝试这样做。你不应该想要它。您从数据库中检索了三个有意义的列表,这些列表中的项目完全不相关。
现在您想要并排显示三个列表,这是一个演示问题。制作一个表格,使用三列,随便什么。 不要试图解决数据库中的表示问题。曾经。
您的数据库是关于数据的。无论您是想并排显示三个列表,一个显示在另一个列表下,还是在三个不同的页面或网站上,检索信息的方式应该绝对没有区别,因为这些不同的显示方式没有 关于您要检索的数据!
关于mysql - 如何将三个查询的结果合并为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25885059/