gpt4 book ai didi

mysql。根据 1 个表中的列从 3 个不同的表中选择数据

转载 作者:行者123 更新时间:2023-11-30 23:39:54 24 4
gpt4 key购买 nike

这是我想做的:

我有 3 个表:musicItems、photoItems、textItems
每个表都有不同的列。

Table "musicItems" 
id
Owner_id
audioId
url
duration

Table "photoItems"
id
albumId
userId
photoId
url

Table "textItems"
id
msgText

我还有一张 table

"footprints" 
id
itemType
itemId

我需要从表“footprints”中检索最后 30 个项目,所有属性都取决于 itemType 和 itemId。我该怎么做?

最佳答案

我假设 ItemType 列包含 ItemID 引用的表名

SELECT  *
FROM footprints fp
LEFT OUTER JOIN musicItems mi ON mi.ID = fp.itemID AND fp.ItemType = 'musicItems'
LEFT OUTER JOIN photoItems pi ON pi.ID = fp.itemID AND fp.ItemType = 'photoItems'
LEFT OUTER JOIN textItems ti ON ti.ID = fp.itemID AND fp.ItemType = 'textItems'
LIMIT 30

关于mysql。根据 1 个表中的列从 3 个不同的表中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4315007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com