我有桌子:
流:
+----+-------------------+------------+
| id | channel | bouquet |
+----+-------------------+------------+
| 1 | USA_CH 1 | ["1","28"] |
| 2 | USA_CH 2 | ["1","28"] |
| 11 | USA_CH 3 | ["1"] |
| 12 | USA_CH 4 | ["28"] |
+----+-------------------+------------+
通道:
+----+--------------------------+---------+----------+
| id | sort | bouquet | reseller |
+----+--------------------------+---------+----------+
| 1 | ["m1","1","2","m2","11"] | ["1"] | 0 |
| 27 | ["1","2"] | ["28"] | 0 |
| 39 | [] | ["33"] | 0 |
+----+--------------------------+---------+----------+
标记:
+----+--------+-------+---------+----------+
| id | marker | title | bouquet | reseller |
+----+--------+-------+---------+----------+
| 1 | m1 | MEGA | 1 | 1 |
+----+--------+-------+---------+----------+
| 2 | m2 | XTRA | 1 | 1 |
+----+--------+-------+---------+----------+
需要使用频道表排序字段中的标记来获取频道顺序:
["m1","1","2","m2","11"]
所以我需要得到这样的输出:
+----+-------------------+
| id | channel |
+----+-------------------+
| 1 | MEGA |
| 1 | USA_CH 1 |
| 2 | USA_CH 2 |
| 2 | XTRA |
| 11 | USA_CH 3 |
+----+-------------------+
我尝试使用union SQL:
SELECT s.id, s.channel, m.title FROM streams s, markers m, 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
UNION
SELECT s.id, s.channel, m.title FROM streams s, markers m, channels c WHERE JSON_SEARCH(c.sort, 'one', m.marker) IS NOT NULL AND
JSON_SEARCH(s.bouquet, 'one', '1') IS NOT NULL;
我明白了:
+----+----------+-------+
| id | channel | title |
+----+----------+-------+
| 1 | USA_CH 1 | MEGA |
| 2 | USA_CH 2 | MEGA |
| 11 | USA_CH 3 | XTRA |
+----+----------+-------+
我需要得到这个:
+----+-------------------+
| id | channel |
+----+-------------------+
| 1 | MEGA |
| 1 | USA_CH 1 |
| 2 | USA_CH 2 |
| 2 | XTRA |
| 11 | USA_CH 3 |
+----+-------------------+
我做错了什么?我在mysql手册上看到UNION用于将两个表值合并为一个......所以我想我会得到我需要的结果但是正如你在我的示例代码中看到的那样我总是从每个表中获得每一行值...并且需要在与channel.sort json值对应的值之后的值...我在html5中构建频道ediotor所以我需要获得频道的排序和读取channel.sort中定义的频道名称和标记。
答案 0 :(得分:0)
我终于做到了! Yupiiiii .... :)
SELECT id, sort, channel FROM
(
SELECT s.id, c.sort, s.channel 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
UNION
SELECT m.marker, c.sort, m.channel FROM markers m, channels c, streams s
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
) A
ORDER BY LOCATE(id, CONCAT("'", sort, "'"));
我的输出很好:
+----+--------------------------+----------+
| id | sort | channel |
+----+--------------------------+----------+
| m1 | ["m1","1","2","m2","11"] | MEGA |
| 1 | ["m1","1","2","m2","11"] | USA_CH 1 |
| 2 | ["m1","1","2","m2","11"] | USA_CH 2 |
| m2 | ["m1","1","2","m2","11"] | XTRA |
| 11 | ["m1","1","2","m2","11"] | USA_CH 3 |
+----+--------------------------+----------+
感谢各位评论上面的例子我在UNION中的单个查询是错误的...使用细节尝试和错误我最终得到它..如果有人需要代码在这里是