Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php/Mysql:CAST/CASE if row+'_拆下';然后存在don';t选择_Php_Mysql_Sql_Casting_Case - Fatal编程技术网

Php/Mysql:CAST/CASE if row+'_拆下';然后存在don';t选择

Php/Mysql:CAST/CASE if row+'_拆下';然后存在don';t选择,php,mysql,sql,casting,case,Php,Mysql,Sql,Casting,Case,我想在我的查询中包含一个非常具体的CAST/CASE,但我真的不知道怎么做。以下是我的SQL行: SELECT * FROM table WHERE ( type = 'aaa' OR type = 'bbb' OR type = 'ccc' OR type = 'ddd') AND (points NOT LIKE '-%') ORDER BY id DESC LIMIT 10

我想在我的查询中包含一个非常具体的
CAST/CASE
,但我真的不知道怎么做。以下是我的SQL行:

SELECT * FROM table WHERE (

         type = 'aaa' OR
         type = 'bbb' OR
         type = 'ccc' OR
         type = 'ddd')

         AND (points NOT LIKE '-%')

         ORDER BY id DESC LIMIT 10
我想说的是:

if   : 'type'_remove EXISTS and 'type'->data == 'type'_remove->data
then : Don't select the 'type' row.
示例,以及我希望查询选择或不选择的内容:

id    type          data  points
----------------------------------
1     aaa            1      1     don't select : aaa_remove exists and data are the same
2     bbb            1      3     select       : bbb_remove exists BUT data aren't the same
3     ddd            1     -1     don't select : points IS LIKE '-%'
4     aaa_remove     1     -1
5     ddd            1     -3     don't select : points IS LIKE '-%'
6     bbb_remove     2     -1
7     ccc            1      1     select       : ccc_remove doesn't exists with the same data

我建议使用一个名为
removed
的字段,这将使整个过程更容易/更清晰,但如果您有自己的理由,您可以尝试以下方法:

SELECT *
FROM `table` AS `outer`
WHERE
    `outer`.`type` IN('aaa', 'bbb', 'ccc', 'ddd') AND
    `outer`.`points` >= 0 AND
    (
        SELECT `inner`.`id`
        FROM `table` AS `inner`
        WHERE
            `inner`.`type` = CONCAT(`outer`.`type`, '_remove')  AND
            `inner`.`data` = `outer`.`data`
        LIMIT 1
    ) IS NULL
ORDER BY `outer`.`id` DESC
LIMIT 10;

此外,如果要对许多类型进行相同的基本设置,您可以只选择
类型而不是“%\u remove”
,而不是列出所需的所有类型。

如果points是数字数据类型,您只需检查
<0
;如果不是数字,为什么不是?酷!是的,积分是一个BIGINT(20),所以是的,我要这么做:)ThinxThink,我想你这家伙!我正在测试它