• 我们中的有些人,注定要在日常生活的点滴中去寻找生命的意义。---- 《生活大爆炸》|

MySQL根据字段值不同连不同表查询

未分类 admin 6个月前 (11-20) 41次浏览 未收录
直接上代码

SELECT
c.id,c.user_id,c.create_time,c.content,u.user_nickname,
CASE c.table_name WHEN 'lesson_post' THEN
(SELECT post_title FROM zhenjiu_lesson_post lp WHERE c.object_id = lp.id)
WHEN 'share_post' THEN
(SELECT post_title FROM zhenjiu_share_post sp WHERE c.object_id = sp.id)
WHEN 'news_post' THEN
(SELECT post_title FROM zhenjiu_news_post np WHERE c.object_id = np.id)
WHEN 'video_post' THEN
(SELECT post_title FROM zhenjiu_video_post vp WHERE c.object_id = vp.id)
ELSE 'XXXX' END AS post_title
{$where} FROM zhenjiu_comment c LEFT JOIN zhenjiu_user u ON c.user_id = u.id
ORDER BY c.create_time DESC;


本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MySQL根据字段值不同连不同表查询
喜欢 (0)