sqlite 使用group_concat [closed]从3个表中查询

gwo2fgha  于 2023-04-21  发布在  SQLite
关注(0)|答案(1)|浏览(139)

已关闭,该问题需要details or clarity,目前不接受回答。
**想要改进此问题?**通过editing this post添加详细信息并澄清问题。

5天前关闭。
Improve this question
这一次,我刚刚学习sql语句,我的数据库中有更多的表

cur.execute('''
CREATE TABLE movie(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    tmdb int NOT NULL,
    o_title text NOT NULL,
    m_title text NOT NULL,
    e_title text NOT NULL)
''')
cur.execute('''
CREATE TABLE country(
    tmdb int NOT NULL,
    country text NOT NULL)
''')

cur.execute('''
CREATE TABLE srt(
    tmdb int NOT NULL,
    lang text NOT NULL)
''')

and I have a query查询:

select *, 
    (select group_concat(country) from country where country.tmdb = movie.tmdb),
    (select group_concat(lang) from srt where srt.tmdb = movie.tmdb)
from movie;

这给予了我很好的结果:

1|517987|Rafiki| Barátnők|Rafiki|ken,zar|en,hu
2|41261|1リットルの涙|1 liter könny|1 litre of Tears|jpn|en,hu
3|365283|残穢-住んではいけない部屋-|A kitörölhetetlen|The Inerasable|jpn|en,hu

但我的问题是这是最好的-最优的SQL查询?
对不起,我的英语不好。
Maven解答

jucafojl

jucafojl1#

您必须进行基准测试以确定哪种方式更快,但另一种编写方法是使用连接。

SELECT m.*, GROUP_CONCAT(DISTINCT c.country) AS countries, GROUP_CONCAT(DISTINCT s.lang) AS languages
FROM movie AS m
LEFT JOIN country AS c ON c.tmdb = m.tmdb
LEFT JOIN srt AS s ON s.tmdb = m.tmdb
GROUP BY m.id

相关问题