• 欢迎访问duantianhen的网站,主要总结PHP、Go、服务器、前端、推荐使用最新版火狐浏览器和Chrome浏览器访问本网站

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

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

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)