在數據庫之外我試圖將兩個表合併成一個搜索查詢。innerjoin或另一種組合數據的方式
表1:常見問題
| faq_id | title | status | url_key |
| ------ | ----- | ------ | ------- |
| 1 | Test | 1 | go.html |
表2:faq_value
| faq_value_id | faq_id | attribute | value |
| ------------ | ------ | --------- | ------- |
| 1 | 1 | metakey | testing |
| 2 | 1 | metadesc | this is a test |
| 3 | 1 | meta_image | test.jpg |
現在我想要的查詢結果是這樣的:
| faq_id | title | status | metadesc | metakey | meta_image | url_key |
| ------ | ----- | ------ | -------- | ------- | ---------- | ------- |
| 1 | Test | 1 | this is a | testing | test.jpg | go.html |
我嘗試到目前爲止已失敗:
SELECT
faq.faq_id,faq.title,status,
(SELECT faq_value.value AS metadesc WHERE faq_value.attribute_code = 'metadesc'),
(SELECT faq_value.value AS metakey WHERE faq_value.attribute_code = 'metakey'),
(SELECT faq_value.value AS meta_image FROM faq_value WHERE faq_value.attribute_code = 'meta_image')
faq.url_key,
FROM faq
INNER JOIN faq_value ON faq_value.faq_id = faq.faq_id
我相信我只是俯視愚蠢的東西,但只是不能發現。這一個踢一個SQL錯誤:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE faq_value.attribute_code = 'metadesc'), (SELECT faq_value.value AS metakey' at line 2
非常感謝你。這解決了我的問題 – Jacco