我想创建一个页面来索引我拥有的所有标题。
我想为每个标题打印首字母。
例如我有这样的标题
所以,我想创建这样的索引
A
atitle1
atitle2
B
btitle3
V
vtitle4
这是我尝试的
SELECT * FROM master_post,
category_relation,
category WHERE category_relation.id_mp = master_post.id_mp
AND category_relation.id_c = category.id_c
AND category.slug_c = 'dorama' AND 1 ORDER BY master_post.judul_mp ASC
我不确定,但我认为使用Distinct
。我在这里Distinct
创建的
SELECT DISTINCT LEFT(judul_mp, 1) AS firstletter FROM master_post WHERE judul_mp = :judul_mp
但是我不知道如何加入我的distinct
代码,我尝试了但是出错。
答案 0 :(得分:1)
您可以这样做:
SELECT mp.judul_mp as str
FROM master_post mp JOIN
category_relation cr
ON mp.id_mp = cr.id_mp JOIN
category c
ON cr.id_c = categcry.id_c
WHERE c.slug_c = 'dorama'
UNION ALL
SELECT DISTINCT LEFT(mp.judul_mp, 1)
FROM master_post mp JOIN
category_relation cr
ON mp.id_mp = cr.id_mp JOIN
category c
ON cr.id_c = categcry.id_c
WHERE c.slug_c = 'dorama'
ORDER BY str;
请注意使用正确的,明确的,标准 JOIN
语法。请勿在{{1}}子句中使用逗号。