g_id Descending  ga_id  ga_gid  ga_ca_id  ga_value
58698  51  58698  207  b
58698  50  58698  207  a
58698  49  58698  178  blue
58698  48  58698  178  red
58697  NULL  NULL  NULL  NULL
58690  39  58690  207  000
58690  38  58690  178  blue
58689  5  58689  178  red
58689  6  58689  178  green
58689  7  58689  207  属性5
要结果:
g_id 即符合ga_id207=a又ga_id178=red
以上表只有58698符合高手求助?????

解决方案 »

  1.   

    详细说明
    SELECT g_id from tth a where ga_gid='207' and ga_ca_id='a'
    and exists(select 1 from tth where a.ga_id=ga_id and ga_gid='178' and ga_ca_id='red')
      

  2.   

    select a.*
    from table1 a inner join  table1 b on a.g_id=b.g_id
    where a.ga_id=207 and b.ga_id=178
    and a.ga_value='a' and b.ga_value='red'
      

  3.   

    楼上可否测过出不来。。冒似,,要几对查询要join 几次了???
      

  4.   

    复杂的是上边那个表是我联合查询出来的结果:
    SELECT g.g_id, ga. *
    FROM goods_chs AS g
    LEFT JOIN goods_attribute AS ga ON ( ga.ga_gid = g.g_id )难道我的错???
      

  5.   

    g_id Descending ga_id ga_gid ga_ca_id ga_value:5个字段还是6个?
      

  6.   

    复楼上。6个
    你的方法看起来很清晰,可还是没调出来
    select a.ga_id
    from goods_attribute a where a.ga_ca_id='178' and a.ga_value='red'
    and exists(select b.ga_id
    from goods_attribute b where a.ga_id=b.ga_id and b.ga_ca_id='207' and b.ga_value='a')
      

  7.   

    许多重复的g_id为什么都不用,group by 呢
      

  8.   

    mysql> SELECT * FROM TTH1;
    +------+-------+------------+-------+--------+----------+----------+
    | ID   | g_id  | Descending | ga_id | ga_gid | ga_ca_id | ga_value |
    +------+-------+------------+-------+--------+----------+----------+
    |    1 | 58698 | 51         | 58698 | 207    | b        | NULL     |
    |    2 | 58698 | 50         | 58698 | 207    | a        | NULL     |
    |    3 | 58698 | 49         | 58698 | 178    | blue     | NULL     |
    |    4 | 58698 | 48         | 58698 | 178    | red      | NULL     |
    |    5 | 58697 | NULL       | NULL  | NULL   | NULL     | NULL     |
    |    6 | 58690 | 39         | 58690 | 207    | 000      | NULL     |
    |    7 | 58690 | 38         | 58690 | 178    | blue     | NULL     |
    |    8 | 58689 | 5          | 58689 | 178    | red      | NULL     |
    |    9 | 58689 | 6          | 58689 | 178    | green    | NULL     |
    |   10 | 58689 | 7          | 58689 | 207    | 灞炴€?    | NULL     |
    +------+-------+------------+-------+--------+----------+----------+
    10 rows in set (0.00 sec)mysql> SELECT g_id from tth1 a where ga_gid='207' and ga_ca_id='a'
        -> and exists(select 1 from tth1 where a.ga_id=ga_id and ga_gid='178' and ga
    _ca_id='red');
    +-------+
    | g_id  |
    +-------+
    | 58698 |
    +-------+
    1 row in set (0.00 sec)mysql>
      

  9.   

    怎么把那联合语句加进去呀
    select a.ga_id
    from goods_attribute a where a.ga_ca_id='178' and a.ga_value='red'
    and exists(select b.ga_id
    from goods_attribute b where a.ga_id=b.ga_id and b.ga_ca_id='207' and b.ga_value='a')
    ||
    ||
    thh1
      

  10.   

     SELECT g.g_id, ga. *
    FROM goods_chs AS g
    LEFT JOIN goods_attribute AS ga ON ( ga.ga_gid = g.g_id )
    WHERE ga.ga_ca_id = '178'
    AND ga.ga_value = 'red'
    AND EXISTS (SELECT b.g_id, gab. *
    FROM goods_chs AS b
    LEFT JOIN goods_attribute AS gab ON ( gab.ga_gid = b.g_id )
    WHERE g.g_id = b.g_id
    AND gab.ga_ca_id = '207'
    AND gab.ga_value = 'a'
    )MySQL said: Documentation
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT b.g_id, gab. *