,我有以下數據模型:如何做一個子查詢GROUP_CONCAT
`title`
- id
- name
`version`
- id
- name
- title_id
`version_price`
- id
- version_id
- store
- price
這裏是數據的一個例子:
`title`
- id=1, name=titanic
- id=2, name=avatar
`version`
- id=1, name="titanic (dubbed in spanish)", title_id=1
- id=2, name="avatar directors cut", title_id=2
- id=3, name="avatar theatrical", title_id=2
`version_price`
- id=1, version_id=1, store=itunes, price=$4.99
- id=1, version_id=1, store=google, price=$4.99
- id=1, version_id=2, store=itunes, price=$5.99
- id=1, version_id=3, store=itunes, price=$5.99
我想建立一個查詢,這將使我所有在iTunes上有version_price但在Google上沒有的標題。我將如何做到這一點?以下是我迄今爲止:
select
title.id, title.name, group_concat(distinct store order by store)
from
version inner join title on version.title_id=title.id inner join version_price on version_price.version_id=version.id
group by
title_id
這給了我GROUP_CONCAT這說明我是什麼,我有:
id name group_concat(distinct store order by store)
1 Titanic Google,iTunes
2 Avatar iTunes
但我怎麼構建查詢到包括該項目是否是在谷歌(使用CASE語句,Or的任何需要的)
id name group_concat(distinct store order by store) on_google
1 Titanic Google,iTunes true
2 Avatar iTunes false
這將基本上是做了group_concat LIKE '%google%'
,而不是一個正常的where子句。
下面是當前查詢的SQL撥弄一個鏈接,我有:http://sqlfiddle.com/#!9/e52b53/1/0
'where store <>'google''?除了「不同」的東西外,並不是group_concat的工作是爲你過濾東西。過濾在'where'子句中完成。 –
你期望得到什麼? '1 |泰坦尼克號Google,iTunes'或者只是'1 |泰坦尼克號iTunes'? – Alex
@MarcB請看更新的問題。 – David542