这里我有两张桌子。我想知道如果一行为null,如何选择另一行。 这是页面表
ut.getlocal(offset)
#=> 2018-06-16 22:39:09 +0200
这是content_descriptions表
+----+-----------+
| id | page_type |
+----+-----------+
| 10 | product |
| 12 | product |
+----+-----------+
这里我想从与content_descriptions表相关的页面中选择,其中language_code是'en'。但是如果不同的page_id没有'en'smanage_code,则必须选择该page_id,尽管language_code是不同的。 我想得到这样的结果。但我不能。
+----+---------+---------------+-----------------------------------------+
| id | page_id | language_code | title |
+----+---------+---------------+-----------------------------------------+
| 8 | 10 | id | Cras dictum, tortor et faucibus feugiat |
| 11 | 10 | en | Cras dictum, tortor et faucibus feugiat |
| 13 | 12 | md | Nam pretium nunc massa, nec viverra |
+----+---------+---------------+-----------------------------------------+
之前谢谢
答案 0 :(得分:0)
Select a.id, a.page_type,
if(b.language_code='en',b.title,b.title) as title from pages a,
content_descriptions b where a.id=b.page_id
这可能会这样做。
答案 1 :(得分:0)
select
p.id,p.page_type,cd.title,case when cd.language_code != 'en' then cd.language_code end
from pages p
inner join content_descriptions cd on p.id = cd.page_id
where cd.language_code = 'en'
现在你可能需要记住:group by p.id
最后
答案 2 :(得分:0)
以下查询产生所需的结果:
sumOFXandY = x + y
SELECT p.*,
IF(cd_en.language_code IS NOT NULL, cd_en.title, cd.title) AS title,
IF(cd_en.language_code IS NOT NULL, cd_en.language_code, cd.language_code) AS language_code
FROM pages AS p
LEFT JOIN content_descriptions AS cd_en
ON (cd_en.page_id = p.id AND cd_en.language_code = 'en')
LEFT JOIN content_descriptions AS cd
ON (cd.page_id = p.id)
GROUP BY p.id
SELECT
检查是否存在英文标题,如果存在,则使用该标题,否则使用随机语言中的其他标题(在这种情况下,您没有指定行为)。IF(cd_en.language_code IS NOT NULL, cd_en.title, cd.title) AS title
language_code
FROM
首先将内容的英文版本加入页面LEFT JOIN content_descriptions AS cd_en ON (cd_en.page_id = p.id AND cd_en.language_code = 'en')
加入后备语言LEFT JOIN content_descriptions AS cd ON (cd.page_id = p.id)
确保每页只返回一次