试试这个:
SELECT c.cat_name FROM post as p
LEFT JOIN cat_list AS c
ON c.cat_id = p.cat_id
WHERE p.post_id = "3"
样本数据:
CREATE TABLE cat_list(
cat_id int auto_increment primary key,
cat_name varchar(30)
);
CREATE TABLE post (
post_id int auto_increment primary key,
cat_id int,
post varchar(30)
);
INSERT INTO cat_list (cat_name)
VALUES ("cat1"),("cat2"), ("cat3"),("cat4"),("cat5"),("cat6"),("cat7"),("cat8");
INSERT INTO post (cat_id, post)
VALUES (1,"jnkljlkajlkjaoiuweiuoi"), (1,"wuoieuoiuoidudjhkj"), (2,"eiuuqueiuriewuqyiuu"), (8,"vzbvbxvnbxmnbmn nmncmn");
SQL 小提琴演示
更改帖子的 ID 以选择类别。