-1

假设我有两张表,一张是咒语列表,另一张是魔咒,即用户选择学习或已经学习的咒语列表。

mysql> SHOW COLUMNS FROM Grimory;
+--------------+------------+------+-----+---------+----------------+
| Field        | Type       | Null | Key | Default | Extra          |
+--------------+------------+------+-----+---------+----------------+
| id           | int(11)    | NO   | PRI | NULL    | auto_increment |
| personage_id | int(11)    | YES  | MUL | NULL    |                |
| spell_id     | int(11)    | YES  | MUL | NULL    |                |
| isLearned    | tinyint(1) | NO   |     | NULL    |                |
| isSelected   | tinyint(1) | NO   |     | NULL    |                |
+--------------+------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM Spell;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| name            | varchar(255) | NO   |     | NULL    |                |
| description     | longtext     | NO   |     | NULL    |                |
| chatDescription | longtext     | NO   |     | NULL    |                |
| level           | int(11)      | NO   |     | NULL    |                |
| isActive        | tinyint(1)   | NO   |     | NULL    |                |
| category_id     | int(11)      | YES  | MUL | NULL    |                |
| createdAt       | datetime     | YES  |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
8 rows in set (0.00 sec)

问题。

我想按 category_id 显示一个包含所有法术的列表,但是对于我想显示的每一行是否该法术是由当前用户(personage_id)学习或选择的。

你能帮我写一个正确的查询吗?

这是如何工作的: 使用 php 我生成:咒语列表 | 复选框被选中 | 复选框已研究

当我单击 isSelected 复选框时,将在 grimory 中添加一条记录,其中包含当前用户和拼写。

4

2 回答 2

2

更新

SELECT a.*, IFNULL(b.isLearned,0) as isLearned, 
IFNULL(b.isSelected,0) as isSelected
FROM Spell a 
LEFT JOIN Grimory b(ON b.spell_id =a.id 
   AND b.personage_id =:current_user_id)
WHERE a.category_id = :current_category_id
于 2013-08-11T16:21:56.870 回答
0
SELECT * FROM Spell 
INNER JOIN Grimory
ON Spell.id = Grimory.spell_id 
WHERE (Grimory.isLearned = 1 OR Grimory.isSelected = 1)
AND Spell.category_id = 'YOUR CAT ID IS HERE'
于 2013-08-11T16:58:17.803 回答