尝试乘以表中的多个值

时间:2018-07-09 19:20:17

标签: mysql

我一天中的大部分时间都在阅读并试图弄清楚这一点。当长度X宽度X高度=> 4000

时,我想显示post_id的列表

表中的meta_value类型为LONGTEXT,因此在进行任何乘法之前必须为CAST。我认为也许我的SQL操作顺序是错误的。自从我做完这件事已经有一段时间了。

SELECT post_id, meta_key
FROM postmeta
WHERE (
        DECLARE @temp_length = (SELECT meta_value
                              FROM postmeta
                             WHERE meta_key = '_length'),
         @temp_width = (SELECT meta_value
                          FROM postmeta
                         WHERE meta_key = '_width'),

         @temp_height = (SELECT meta_value
                          FROM postmeta
                         WHERE meta_key = '_height')
        AND

    (CAST(@temp_length AS UNSIGNED) * CAST(@temp_width AS UNSIGNED) * CAST(@temp_height AS UNSIGNED) > 250))
ORDER BY post_id;

我收到错误消息:

MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE @temp_length = (SELECT meta_value
                                  FRO' at line 4

1 个答案:

答案 0 :(得分:0)

使用查询将每个值放入单独的列中。然后使用HAVING根据产品对其进行过滤。

SELECT post_id,
    CAST(MAX(CASE WHEN meta_key = '_length' THEN meta_value END) AS UNSIGNED) AS temp_length,
    CAST(MAX(CASE WHEN meta_key = '_width' THEN meta_value END) AS UNSIGNED) AS temp_width,
    CAST(MAX(CASE WHEN meta_key = '_height' THEN meta_value END) AS UNSIGNED) AS temp_height
FROM postmeta
GROUP BY post_id
HAVING temp_length * temp_width * temp_height >= 4000