使用SELECT有条件地填充列

时间:2013-10-30 21:09:18

标签: mysql relational-database conditional-statements

我想从我的数据库中获取一些表,并将它们组合成一个结果查询。

我有一个products表:

+-----------------+-----------------------------+------+-----+---------+----------------+
| Field           | Type                        | Null | Key | Default | Extra          |
+-----------------+-----------------------------+------+-----+---------+----------------+
| id              | bigint(20)                  | NO   | PRI | NULL    | auto_increment |
| menu_id         | bigint(20)                  | YES  |     | NULL    |                |
| menu_is_section | int(1)                      | NO   |     | 1       |                |
| company_id      | bigint(20)                  | NO   | MUL | NULL    |                |
| name            | varchar(255)                | NO   |     | NULL    |                |
| description     | mediumtext                  | YES  |     | NULL    |                |
| image           | varchar(255)                | NO   |     | NULL    |                |
| initial_time    | datetime                    | YES  |     | NULL    |                |
| final_time      | datetime                    | YES  |     | NULL    |                |
| archived        | int(1)                      | NO   |     | 0       |                |
| full_image      | varchar(255)                | YES  |     | NULL    |                |
| kind            | enum('PRODUCT','PROMOTION') | NO   |     | PRODUCT |                |
+-----------------+-----------------------------+------+-----+---------+----------------+

product_likes表:

+------------+------------+------+-----+-------------------+-----------------------------+
| Field      | Type       | Null | Key | Default           | Extra                       |
+------------+------------+------+-----+-------------------+-----------------------------+
| user_id    | bigint(20) | NO   | MUL | NULL              |                             |
| product_id | bigint(20) | NO   | MUL | NULL              |                             |
| liked_on   | timestamp  | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+------------+------------+------+-----+-------------------+-----------------------------+

users表,其唯一相关字段为id

我想获取菜单下的所有产品,并确定用户是否“喜欢”给定产品。为此,我正在考虑进行连接以创建一个新列,该列将创建包含1或0的“喜欢”列,以查看用户是否喜欢某个产品。

但我有点卡住了,因为我找不到用MySQL有条件地填充列的方法。

到目前为止,我的查询如下:

"SELECT id, name, description, concat('$image_base_url/products/', image) AS icon, concat('$image_base_url/products_full/', full_image) AS full_image FROM (SELECT * FROM products WHERE menu_id = ? AND menu_is_section = ?) AS selected WHERE UTC_TIMESTAMP() BETWEEN initial_time AND final_time ORDER BY initial_time DESC"

我需要完成的任务是查找user_id是否喜欢product_it,并相应地创建列。

最好的方法是什么?

编辑:

我刚做了LEFT JOIN,我想我已经接近我想要的了。 “likes”返回1,但我99%肯定是因为user_id是1.当用户不喜欢产品时,它返回NULL。我可以使用它,但如果我的Web服务器返回1和0,那会更好。

"SELECT selected.id, u_likes.user_id AS liked, selected.name, selected.description, concat('$image_base_url/products/', selected.image) AS icon, concat('$image_base_url/products_full/', selected.full_image) AS full_image FROM (SELECT * FROM products WHERE menu_id = ? AND menu_is_section = ?) AS selected LEFT JOIN (SELECT * FROM product_likes WHERE user_id = ?) AS u_likes ON selected.id = u_likes.product_id WHERE UTC_TIMESTAMP() BETWEEN selected.initial_time AND selected.final_time ORDER BY selected.initial_time DESC"

1 个答案:

答案 0 :(得分:0)

我相信我已经弄明白了。如果这里有专家,请查看我的解决方案:

SELECT selected.id, CASE WHEN u_likes.user_id IS NULL THEN '0' ELSE '1' END AS liked, selected.name, selected.description, concat('$image_base_url/products/', selected.image) AS icon, concat('$image_base_url/products_full/', selected.full_image) AS full_image FROM (SELECT * FROM products WHERE menu_id = ? AND menu_is_section = ?) AS selected LEFT JOIN (SELECT * FROM product_likes WHERE user_id = ?) AS u_likes ON selected.id = u_likes.product_id WHERE UTC_TIMESTAMP() BETWEEN selected.initial_time AND selected.final_time ORDER BY selected.initial_time DESC

似乎正确地将1或0放在一个名为“喜欢”的字段中。可悲的是,我现在不能做很多测试用例,但看起来它工作正常。