简体   繁体   English

加入2个MySQL查询,结果相同

[英]Join 2 Mysql query and result in the same

I am a newbie to this coding world. 我是这个编码世界的新手。 Is it possible to join two Queries into one? 是否可以将两个查询合并为一个? I have googled but I didn't get what exactly I am looking for so I reached here: My first query is : 我已经用谷歌搜索,但是我没有得到我想要的东西,所以我到达了这里:我的第一个查询是:

SELECT filename
     , course.cname
     , DATE_FORMAT(pd_course_file.creationDate,"%D %M %Y") as date
     , type 
  FROM pd_course_file
  JOIN course 
    ON pd_course_file.course_id = course.id

Now whatever the result of type column for example the result of type I got pdf then my next query will be 现在,无论类型列的结果如何,例如我得到pdf的类型的结果,那么我的下一个查询就是

Select icons from fileType  where type = 'pdf'

How can I join both queries? 如何加入两个查询? Any help will be highly appreciated. 任何帮助将不胜感激。

if the type is the file type you could use a join 如果类型是文件类型,则可以使用联接

SELECT filename
  , course.cname
  ,DATE_FORMAT(pd_course_file.creationDate,"%D %M %Y") as date
  , type 
  , ft.icon 
FROM pd_course_file p
INNER JOIN course ON pd_course_file.course_id = course.id 
INNER JOIN fileType ft ON ft.type = pd_course_file.type  and ft.type='pdf'

oterwise if ever is a pdf but you have not a relatio you could use a cross join 否则,如果曾经是pdf,但没有关联,则可以使用交叉联接

SELECT filename
  , course.cname
  ,DATE_FORMAT(pd_course_file.creationDate,"%D %M %Y") as date
  , type 
  , ft.icon 
FROM pd_course_file p
INNER JOIN course ON pd_course_file.course_id = course.id 
CROSS JOIN fileType ft
where ft.type = 'pdf'

You could add a subquery as part of the select. 您可以在选择中添加子查询。 Like so... 像这样

SELECT filename
     , course.cname
     , DATE_FORMAT(pd_course_file.creationDate,"%D %M %Y") as date
     , type
     , (select icons from filetype where type = 'PDF') as icons
  FROM pd_course_file
  JOIN course 
    ON pd_course_file.course_id = course.id

But that will get you a list of all the icons from the fileType table in put them in the icons column. 但这将为您提供fileType表中所有图标的列表,并将它们放在icons列中。 If there's something else to key off of in the fileType table, you'd want to add that to the selecting subquery. 如果在fileType表中还有其他需要输入的内容,则需要将其添加到选择子查询中。 For example: 例如:

SELECT filename
     , course.cname
     , DATE_FORMAT(pd_course_file.creationDate,"%D %M %Y") as date
     , type
     , (select icon from filetype ft where ft.type = 'PDF' and ft.file_course_id = pd_course_file.course_id) as icon
  FROM pd_course_file
  JOIN course 
    ON pd_course_file.course_id = course.id

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

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