duanjiwu0324 2017-07-07 08:04
浏览 18

mysql如何获取组有值

Hi I'm mysql table like this

mysql> mysql> SELECT ID,type FROM sys_tags; +----+------+ | ID | type | +----+------+ | 1 | 0 | | 2 | 0 | | 3 | 0 | | 4 | 0 | | 5 | 0 | | 6 | 0 | | 7 | 0 | | 8 | 0 | | 9 | 0 | | 10 | 1 | | 11 | 1 | | 12 | 1 | | 13 | 1 | | 14 | 1 | | 15 | 1 | | 16 | 1 | | 17 | 1 | +----+------+ 15 rows in set (0,00 sec)

mysql> SELECT * FROM `sys_tags_groups`;
+----+----------+--------+--------+------+
| ID | ID_group | ID_row | id_tag | type |
+----+----------+--------+--------+------+
|  0 |     3123 |      1 |     10 |    1 |
|  0 |     3123 |      2 |     11 |    1 |
|  0 |     3123 |      3 |      6 |    0 |
|  0 |     3123 |      4 |      2 |    0 |
|  1 |     3123 |      5 |     13 |    1 |
|  1 |     3123 |      6 |      3 |    0 |
|  0 |     5142 |      7 |     12 |    1 |
|  0 |     5142 |      8 |     15 |    1 |
|  0 |     5142 |      9 |      6 |    0 |
|  0 |     5142 |     10 |      2 |    0 |
+----+----------+--------+--------+------+
10 rows in set (0,00 sec)

okey I'm try explaining what I mean example

+----+----------+--------+--------+------+ | ID | ID_group | ID_row | id_tag | type | +----+----------+--------+--------+------+ | 0 | 3123 | 1 | 10 | 1 | | 0 | 3123 | 2 | 11 | 1 | | 0 | 3123 | 3 | 6 | 0 | | 0 | 3123 | 4 | 2 | 0 |

its one group see row type for this group I'm have 2 row with type 1 and 2 row with type 0, next I'm wanna get this group if I'm have tags with type 1 and type 0 example my enter values

  • id_tag = 6 (type = 0)
  • id_tag = 10 (type = 1)

example record have 1 tag from group type = 1 and one tag from group with type = 0 its true

+----+----------+--------+--------+------+ | ID | ID_group | ID_row | id_tag | type | +----+----------+--------+--------+------+ | 0 | 3123 | 1 | 10 | 1 | <-- success have tag with type = 1 | 0 | 3123 | 2 | 11 | 1 | | 0 | 3123 | 3 | 6 | 0 | <-- success have tag with type = 0 | 0 | 3123 | 4 | 2 | 0 |

next

example my enter values

  • id_tag = 6 (type = 0)
  • id_tag = 15 (type = 1)

how you see my record have one tag from group 0 but not one tags from group 1 its false

+----+----------+--------+--------+------+ | ID | ID_group | ID_row | id_tag | type | +----+----------+--------+--------+------+ | 0 | 3123 | 1 | 10 | 1 | <---- no one tag with type 1 | 0 | 3123 | 2 | 11 | 1 | <---- no one tag with type 1 | 0 | 3123 | 3 | 6 | 0 | | 0 | 3123 | 4 | 2 | 0 |

next example

example my enter values - id_tag = 6 (type = 0) - id_tag = 2 (type = 0) - id_tag = 15 (type = 1) - id_tag = 11 (type = 1)

okey here I'm have one or more tags from tags with type = 0 and one or more from type = 1 its true

+----+----------+--------+--------+------+ | ID | ID_group | ID_row | id_tag | type | +----+----------+--------+--------+------+ | 0 | 3123 | 1 | 10 | 1 | | 0 | 3123 | 2 | 11 | 1 | | 0 | 3123 | 3 | 6 | 0 | | 0 | 3123 | 4 | 2 | 0 |

example my enter values - id_tag = 7 (type = 0) - id_tag = 3 (type = 0) - id_tag = 15 (type = 1) - id_tag = 11 (type = 1)

its false

+----+----------+--------+--------+------+ | ID | ID_group | ID_row | id_tag | type | +----+----------+--------+--------+------+ | 0 | 3123 | 1 | 10 | 1 | | 0 | 3123 | 2 | 11 | 1 | | 0 | 3123 | 3 | 6 | 0 | <--- not have tags with type 0 | 0 | 3123 | 4 | 2 | 0 | <--- not have tags with type 0

How do I correctly create a request to mysql ?

UPD I think I found way

$enterVals = [7,3,15,11];
for ($i = 0; $i < count($enterVals);$i++) {
    $tagsWHERE[] = "id_tag=".intval($enterVals[$i]);
}
$sql->("SELECT ID_group,ID
FROM sys_tags_groups
WHERE ".implode(" OR ",$tagsWHERE)." 
GROUP BY ID_group,ID
HAVING SUM(type = 1)  AND SUM(type = 0)")

$result = [];

$enterVals = [6,2,15,11];
$result = [{"ID_group":3123,"ID":0},{"ID_group":5142,"ID":0}];

and I'm have next question - how I can get better way ? ) I'm asking because HAVING in request its not better way as I know )

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 测距传感器数据手册i2c
    • ¥15 RPA正常跑,cmd输入cookies跑不出来
    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法