MySQL选择从到并在返回的id结果中排除m

时间:2019-05-24 12:20:31

标签: mysql between

我有从MySQL返回此值的SQL:

SELECT id, channel, (y.count) AS channels
FROM (
    SELECT s.id AS id, c.sort, s.channel
    FROM channels c, streams s
    LEFT JOIN logo l ON s.id=l.channel
    WHERE JSON_SEARCH(s.bouquet, 'one', 1) IS NOT NULL 
    AND JSON_SEARCH(c.bouquet, 'one', 1) IS NOT NULL
    AND JSON_SEARCH(c.sort, 'one', s.id) IS NOT NULL

    UNION 

    SELECT m.marker, c.sort, m.channel
    FROM markers m, channels c, streams s
    LEFT JOIN logo l ON s.id=l.channel
    WHERE JSON_SEARCH(m.bouquet, 'one', 1) IS NOT NULL 
    AND JSON_SEARCH(c.bouquet, 'one', 1) IS NOT NULL
    AND JSON_SEARCH(c.sort, 'one', m.marker) IS NOT NULL
    AND s.id=l.channel) ch
CROSS JOIN (
    SELECT count
    FROM (
        SELECT COUNT(s.id RLIKE '^[0-9]+$') AS count
        FROM streams s, channels c 
        WHERE JSON_SEARCH(s.bouquet, 'one', 1) IS NOT NULL 
        AND JSON_SEARCH(c.bouquet, 'one', 1) IS NOT NULL 
        AND JSON_SEARCH(c.sort, 'one', s.id) IS NOT NULL) AS x) AS y
ORDER BY LOCATE(CONCAT('"', id, '"'), sort);

此查询的返回值为:

+----+-------------------+
| id | channel           |
+----+-------------------+
| m2 | AA-AA             |
| 1  | KR_A 1            |
| 2  | KR_A 2            |
| 11 | HR_A 3            |
| 12 | HR_B 1            |
| m4 | BB-BB             |
| m3 | CC-CC             |
| 17 | SR_B 1            |
| m5 | DD-DD             |
| m1 | EE-EE             |
+----+-------------------+

我需要将SQL修改为在查询中添加m2才能返回:

1
2
11
12

如果我添加m3,我需要获得:

17

因此,我尝试在SQL的开头添加此内容以过滤ID:

SELECT IF (id BETWEEN 'm2' AND '%m', id, 0) As id, channel, (y.count) AS channels

我明白了:

+----+-------------------+----------+
| id | channel           | channels |
+----+-------------------+----------+
| m2 | AA-AA             |        5 |
| 0  | KR_A 1            |        5 |
| 0  | KR_A 2            |        5 |
| 0  | HR_A 3            |        5 |
| 0  | HR_B 1            |        5 |
| m4 | BB-BB             |        5 |
| m3 | CC-CC             |        5 |
| 0  | SR_B 1            |        5 |
| m5 | DD-DD             |        5 |
| m1 | EE-EE             |        5 |
+----+-------------------+----------+

并且需要得到这个:

+----+-------------------+----------+
| id | channel           | channels |
+----+-------------------+----------+
| 1  | KR_A 1            |        4 |
| 2  | KR_A 2            |        4 |
| 11 | HR_A 3            |        4 |
| 12 | HR_B 1            |        4 |
+----+-------------------+----------+

我不知道如何在从'm2'到下一个'm'中选择并从结果中排除'm2''m'的SQL查询中进行操作。...任何帮助?

1 个答案:

答案 0 :(得分:0)

如果上一行是所需的标记(当for z in $(cat /dir/file2) do for f in $(cat /dir/file) do if (( $f=$z )) then (what im asking about) i=0 break else i=1 fi done if ((i=1)) then ...... 以' m '开头时)还是id在所需的标记之后,则可以使用变量进行保存。标记(当id不是以' m '开头并且标记(id)处于活动状态时)

@showRow