Mysql,通过内部选择用where子句

时间:2017-01-26 08:44:55

标签: mysql comma group-concat

我有两张桌子:

tab1(值与tab2的逗号分隔ID):

id1   val1
-----------
1      1
2     1,2
3     1,3

TAB2:

id2   val2
-----------
1      a
2      b
3      c

我想列出Tab1中的值并将逗号分隔的val1替换为val2

我做了这样的事情:

SELECT *, (SELECT GROUP_CONCAT(val2) FROM tab2 WHERE id2 IN val1 ) from tab1

但它会在tab1

附近生成语法错误' val1)

当我删除Where子句时,它完美无缺:

SELECT *, (SELECT GROUP_CONCAT(val2) FROM tab2) from tab1

它产生:

id1   val1  (SELECT GROUP_CONCAT(val2) FROM tab2 )
1     1,2      a,b,c
2     1,3      a,b,c

但我希望第三行字母与Val1号码相对应(只有a,b和a,c)。 Key是使用tab2.val2中的字母替换Tab1.val1:1,2 ...中的数字。它应该产生:

id1   val1  (SELECT GROUP_CONCAT(val2) FROM tab2 WHERE id2 IN val1)
1     1,2      a,b
2     1,3      a,c

当我添加where子句时发生了一些事情,但我无法找到我的错误。

也许还有其他想法用其他表中的字母值替换这个逗号分隔的数字?

2 个答案:

答案 0 :(得分:1)

您可以使用FIND_IN_SET()功能:

    <style>
      .the-modal .modal-content{
          width:500px;
          height:300px;
      }
   </style>


  </head>

  <body ng-app="app" ng-controller="ctl">

    <script type="text/ng-template" id="myModalContent.html">
        <p>Some content</p>
    </script>

    <button type="button" class="btn btn-default" ng-click="open()">Open me!</button>

   </body>

这是一个小提琴:http://rextester.com/ZMFML52150

答案 1 :(得分:0)

如果你可以创建一个函数,那么

drop function if exists `F`;
delimiter //

CREATE DEFINER=`root`@`localhost` FUNCTION `F`(
    `instring` varchar(255)
)
RETURNS varchar(255) CHARSET latin1
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
begin
declare   tempstring varchar(100);
declare   outstring  varchar(100);
declare  val2string varchar(100);
declare  checkit int;
set tempstring = ltrim(rtrim(instring));
set checkit = 0;
set val2string = '';
looper: while   tempstring is not null and instr(tempstring,',') > 0 do
        set outstring = substr(tempstring,1,instr(tempstring, ',') - 1);
        set tempstring = substr(tempstring,instr(tempstring, ',') + 1,length(tempstring) - instr(tempstring, ',') + 1);
        set checkit = 1 ;
        set val2string = concat(val2string,(select val2 from tab2 where id = outstring),',');
end while; 


if checkit = 0 then 
    set val2string = 'Comma not found-no val2'; 
else
    set val2string = concat(val2string,(select val2 from tab2 where id = tempstring));
end if;
return val2string;
end //

delimiter ;

ariaDB [sandbox]> drop table if exists tab1;
Query OK, 0 rows affected (0.09 sec)

MariaDB [sandbox]> drop table if exists tab2;
Query OK, 0 rows affected (0.08 sec)

MariaDB [sandbox]> create table tab1(id int,  val1 varchar(20));
Query OK, 0 rows affected (0.22 sec)

MariaDB [sandbox]> insert into tab1 values
    -> (1  ,    '1'),
    -> (2  ,   '1,2'),
    -> (3  ,   '1,3'),
    -> (4  ,   '1,1,2,2,3,3,3,3');
Query OK, 4 rows affected (0.05 sec)
Records: 4  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> create table tab2(id int,  val2 varchar(20));
Query OK, 0 rows affected (0.30 sec)

MariaDB [sandbox]> insert into tab2 values
    -> (1 ,     'a'),
    -> (2 ,     'b'),
    -> (3 ,     'c');
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [sandbox]>
MariaDB [sandbox]> SELECT ID,`F`(VAL1) FROM TAB1;
+------+-------------------------+
| ID   | `F`(VAL1)               |
+------+-------------------------+
|    1 | Comma not found-no val2 |
|    2 | a,b                     |
|    3 | a,c                     |
|    4 | a,a,b,b,c,c,c,c         |
+------+-------------------------+
4 rows in set (0.00 sec)