Mysql 根据属性值从适当的表中提取数据
我有以下几张表 职位 正文 形象 录像带 我想根据元素类型获取数据。 例如,若元素类型为text,那个么它应该从文本表中获取数据。 如何为此编写查询Mysql 根据属性值从适当的表中提取数据,mysql,Mysql,我有以下几张表 职位 正文 形象 录像带 我想根据元素类型获取数据。 例如,若元素类型为text,那个么它应该从文本表中获取数据。 如何为此编写查询 如果元素类型为文本,则不应看到图像和视频表。它应该只从text和post表中获取数据 即使null select p.*, t.*, i.*, v.* from post p left join text t on t.id = p.element_id and p.element_type = 'text' left join image i o
如果元素类型为文本,则不应看到图像和视频表。它应该只从text和post表中获取数据 即使
null
select p.*, t.*, i.*, v.*
from post p
left join text t on t.id = p.element_id and p.element_type = 'text'
left join image i on i.id = p.element_id and p.element_type = 'image'
left join video v on v.id = p.element_id and p.element_type = 'video'
我不知道。但我认为对于存储在文本表中的文本,这个查询也会遍历图像表和视频表。如果
element\u type
为文本,则不应看到图像和视频表。它应该只从text和post表中获取数据。如果您的表被正确索引,那么这个额外的扫描(由于连接)将是最小的。您可以执行此操作,也可以执行两个查询。我认为没有太多的选择。哪一个比join或两个查询更有效?建议同时运行这两个查询并查看。
id message
1 asdfa sdf
id path
1 asfda/asfd/asdf
id link
1 asdf/asdfasdf
select p.*, t.*, i.*, v.*
from post p
left join text t on t.id = p.element_id and p.element_type = 'text'
left join image i on i.id = p.element_id and p.element_type = 'image'
left join video v on v.id = p.element_id and p.element_type = 'video'