[英]MySQL Union Not Working As Expected
I have tables: 我有桌子:
Streams: 流:
+----+-------------------+------------+
| 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"] |
+----+-------------------+------------+
Channels: 频道:
+----+--------------------------+---------+----------+
| id | sort | bouquet | reseller |
+----+--------------------------+---------+----------+
| 1 | ["m1","1","2","m2","11"] | ["1"] | 0 |
| 27 | ["1","2"] | ["28"] | 0 |
| 39 | [] | ["33"] | 0 |
+----+--------------------------+---------+----------+
Markers: 标记:
+----+--------+-------+---------+----------+
| id | marker | title | bouquet | reseller |
+----+--------+-------+---------+----------+
| 1 | m1 | MEGA | 1 | 1 |
+----+--------+-------+---------+----------+
| 2 | m2 | XTRA | 1 | 1 |
+----+--------+-------+---------+----------+
And need to get channels order with marker from channels table sort field: 并且需要从渠道表排序字段中获取带有标记的渠道订单:
["m1","1","2","m2","11"]
So i need to get output like this: 所以我需要这样的输出:
+----+-------------------+
| id | channel |
+----+-------------------+
| 1 | MEGA |
| 1 | USA_CH 1 |
| 2 | USA_CH 2 |
| 2 | XTRA |
| 11 | USA_CH 3 |
+----+-------------------+
I try using union SQL: 我尝试使用联合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;
And i get this: 我得到这个:
+----+----------+-------+
| id | channel | title |
+----+----------+-------+
| 1 | USA_CH 1 | MEGA |
| 2 | USA_CH 2 | MEGA |
| 11 | USA_CH 3 | XTRA |
+----+----------+-------+
And i need to get this: 我需要得到这个:
+----+-------------------+
| id | channel |
+----+-------------------+
| 1 | MEGA |
| 1 | USA_CH 1 |
| 2 | USA_CH 2 |
| 2 | XTRA |
| 11 | USA_CH 3 |
+----+-------------------+
What i have done wrong? 我做错了什么? I read on mysql manual that UNION is for merging two tables values into one...so i was thinking that i will get this result that i need but as you see in my example code i get always from each table each row value..and needs to be value after value corresponding to channels.sort json value...i im building channel ediotor in html5 so i need to get ordering of channels and reading channels name and markers as defined in channels.sort.
我在mysql手册上读到UNION是用于将两个表值合并为一个...所以我在想我会得到所需的结果,但是正如您在示例代码中看到的那样,我总是从每个表中得到每个行值。并且需要在对应于channel.sort json值的值之后是值。我在html5中建立频道编辑器,因此我需要获取频道的顺序并读取channel.sort中定义的频道名称和标记。
I finally did it! 我终于做到了! Yupiiiii.... :)
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, "'"));
And i got nice output: 而且我得到很好的输出:
+----+--------------------------+----------+
| 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 |
+----+--------------------------+----------+
Thanks guys for commenting above example that my single query in UNION is wrong..using detail try and error i got it finally..so if someone needs code here is it 谢谢大家评论上面的例子,我在UNION中的单个查询是错误的..使用详细的尝试和错误我终于得到了..所以如果有人需要这里的代码是
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.