Mysql列的行以另一行的值开始

时间:2010-02-16 12:40:59

标签: mysql recursive-query

我有一张如下表:

 MyTable
 --------
  A
  B
  C

A,B,C是列,MyTable是表名,我想运行一个mysql查询,如:

SELECT MT1.A, MT2.A, MT2.B FROM MyTable MT1, MyTable MT2
       WHERE MT1.B<>MT2.B and MT2.B like "MT1.B%" and MT2.status=0;

正如您从上面的查询中看到的,我有一个表,我想找到以另一个行值开头并匹配条件的列。但是上面的查询明显失败了,因为mysql将“MT1.B%”作为字符串,我怎么能用Mysql实现呢?

2 个答案:

答案 0 :(得分:3)

您应该能够执行以下操作:

SELECT 
    MT1.A, MT2.A, MT2.B
FROM 
    MyTable MT1, MyTable MT2
WHERE
    MT1.B <> MT2.B AND 
    MT2.B LIKE CONCAT(MT1.B, '%') AND 
    MT2.status = 0

答案 1 :(得分:0)

SELECT  MT1.A, MT2.A, MT2.B
FROM    MyTable MT1, MyTable MT2
WHERE   MT1.B<>MT2.B
        AND MT2.B LIKE CONCAT(MT1.B, '%')
        AND MT2.status=0