Join table depending on value of column in main table(根据主表中列的值连接表)
本文介绍了根据主表中列的值连接表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
notificationTable
ID type typeID userID
==============================================
1 comment 34 2
2 accept 22 2
eventTable
ID event content
===================================
21 post 34
22 accept 22
commentTable
ID comment eventID
===================================
34 'test' 21
我想根据列类型的值连接不同的表.这是我当前的伪代码:
I want to join different tables depending on the value of the column type. This is my current pseudocode:
SELECT *
FROM notificationTable notification
IF (notification.type == 'comment')
LEFT JOIN commentTable comment
ON notification.typeID = comment.ID
ELSEIF (notification.type == 'accept')
LEFT JOIN evenTable event
ON notification.typeID = event.ID
WHERE notification.userID = 2
有人知道我的意图的真正查询吗?
Anyone knows the real query for my intention?
推荐答案
类似
SELECT *
FROM notificationTable notification
LEFT JOIN commentTable comment
ON (notification.typeID = comment.ID AND notification.type == 'comment')
LEFT JOIN evenTable event
ON (notification.typeID = event.ID AND notification.type == 'accept')
WHERE notification.userID = 2
但我强烈建议重新设计你的系统以避免多态关系...
But I strongly recommend to redesign your system to avoid polymorphic relationship...
这篇关于根据主表中列的值连接表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:根据主表中列的值连接表
基础教程推荐
猜你喜欢
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01