1

我正在研究知识产权管理系统,我在数据库中有 2 个表 - 商标和设计。

然后我有反对意见。这意味着,如果其他人拥有与我们客户相似的商标或设计,经理可以制造新的反对意见。

例如,我有 3 个表:

商标:

id 
name

设计:

id
name

反对:

id
name
object_id
object_table

我不知道,与什么表反对有关,但我应该有可能进行这种查询:

SELECT id, name, opposition_object.name FROM opposition
LEFT JOIN (trademark|design) as opposition_object on opposition.object_id =   (trademark|design).id

首先,我考虑将表名存储为对立表的 object_table 列,但后来我意识到我不知道是否可以查询表名,首先,这肯定是一个糟糕的设计。

但是我在这里被击中了,什么都没有出现在我的脑海中。那么有没有人有任何想法如何处理它?

4

2 回答 2

3

也许是这样的:

SELECT 
    id, 
    name, 
    COALESCE(trademark.name,design.name) AS object_name
FROM 
    opposition
    LEFT JOIN trademark 
        on opposition.object_id =   trademark.id
        AND trademark.object_table ='trademark'
    LEFT JOIN design
        on opposition.object_id =   design.id
        AND design.object_table ='design'
于 2012-04-26T10:53:20.017 回答
0
SELECT opposition.id, opposition.name, trademark.name AS object_name
FROM opposition
LEFT JOIN trademark
  ON opposition.object_id = trademark.id
WHERE opposition.object_table = 'trademark'

UNION

SELECT opposition.id, opposition.name, design.name AS object_name
FROM opposition
LEFT JOIN design
  ON opposition.object_id = design.id
WHERE opposition.object_table = 'design'

这发出两个查询:一个加入trademark表和一个加入design。然后使用 将结果统一为一个结果集UNION

于 2012-04-26T10:50:06.093 回答