我有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
答案 0 :(得分:0)
使用SQL创建记录结果集。
在每条记录中,字段都有一定的关系。
现在,在您想要的输出中,我敢说namriaVacantPosition07 17 14b.rar
,im_0710.zip
和ccIlocanoVer.rar
彼此之间有绝对的没有关系,除非它们发生在同一个数据库中的某个地方,如果你把三个不相关的列表放在一起,巧合就会出现在同一行。
如果没有逻辑上的理由来描述为什么这三个东西应该出现在同一行上,那么在SQL中无法做到这一点。
事实上,你应该不甚至尝试这样做。你不应该想要它。您从数据库中检索了三个有意义的列表,这些列表中的项目完全不相关。
现在,您希望显示三个列表并排,这是演示文稿问题。制作一张桌子,使用三列,无论如何。 请勿尝试解决数据库中的演示文稿问题。永远。
您的数据库是关于数据。您检索信息的方式绝对没有区别,无论您是要并排显示三个列表,还是一个在另一个下面,还是在三个不同的页面或网站上,因为这些不同的演示文稿会改变 nothing < / em>关于您要检索的数据!
答案 1 :(得分:0)
可以在rownum上使用完全连接的SQL上完成。 不幸的是,MySQL并不支持完全连接,row_number()和CTE,因此查询有点复杂,但是它有效:
select Maps,Publications,Others from
(
select Maps,Publications, Others from
(
select @r:=@r+1 as 'Num', e.*
from
(
select
Maps
,Others
from
(
SELECT
@m:=@m+1 as 'Num'
,d.fname as 'Maps'
from df d
cross join (select @m:=0) t
where cat_id="M"
order by db
) maps
left join
(
SELECT
@n:=@n+1 as 'Num'
,d.fname as 'Others'
from df d
cross join (select @n:=0) t
where cat_id="O" order by db
) others on maps.Num=others.Num
union all
select
Maps
,Others
from
(
SELECT
@o:=@o+1 as 'Num'
,d.fname as 'Maps'
from df d
cross join (select @o:=0) t
where cat_id="M"
order by db
) maps
right join
(
SELECT
@p:=@p+1 as 'Num'
,d.fname as 'Others'
from df d
cross join (select @p:=0) t
where cat_id="O"
order by db
) others on maps.Num=others.Num
where maps.Num is null
)e
cross join (select @r:=0) t
)x
left join
(
SELECT
@q:=@q+1 as 'Num'
,d.fname as 'Publications'
from df d
cross join (select @q:=0) t
where cat_id="P"
order by db
) pubs on pubs.Num=x.Num
union all
select
Maps
,Publications
,Others
from
(
select
@r1:=@r1+1 as 'Num'
,e.*
from
(
select
Maps
,Others
from
(
SELECT
@m1:=@m1+1 as 'Num'
,d.fname as 'Maps'
from df d
cross join (select @m1:=0) t
where cat_id="M"
order by db
) maps
left join
(SELECT
@n1:=@n1+1 as 'Num'
,d.fname as 'Others'
from df d
cross join (select @n1:=0) t
where cat_id="O"
order by db
) others on maps.Num=others.Num
union all
select
Maps
,Others
from
(
SELECT
@o1:=@o1+1 as 'Num'
,d.fname as 'Maps'
from df d
cross join (select @o1:=0) t
where cat_id="M"
order by db
) maps
right join
(
SELECT
@p1:=@p1+1 as 'Num'
,d.fname as 'Others'
from df d
cross join
(select @p1:=0) t
where cat_id="O"
order by db
) others on maps.Num=others.Num
where maps.Num is null
)e
cross join (select @r1:=0) t
)z
right join
(
SELECT
@q1:=@q1+1 as 'Num'
,d.fname as 'Publications'
from df d
cross join (select @q1:=0) t
where cat_id="P"
order by db
) pubs on pubs.Num=z.Num
where z.Num is null
)y
完全连接用左右连接替换为union all(具有巨大的开销),dt列模拟&#39; date_added&#39;列。