再说一下,这是一个SQL语句,也就是说我再PHP里可以直接调用的。
$str_sql = "你写的SQL语句";
$result = mysql_db_query("pcb", $str_sql); 

解决方案 »

  1.   

    不知到你这三个表是怎么关联的,这样的话只有写两句sql咯~$sql = "select * from a,b,c where c.id=yourvar AND (c.flag=a.flag OR c.flag=b.flag)";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res); ??
      

  2.   

    a,b表各加一个字段与c表的flag对应
      

  3.   

    把abc表的结构写出来看看,不同的情况处理不一样特别是a和b表的机构是否相似
      

  4.   

    好象大家没明白我的意思。我再说详细一点。
    表a,字段有id int, name varchar
    表b,字段有id int, name varchar
    表c,字段有id int auto_increment, flag varchar(实际中,flag的值只会是"y"或者"n")我的要求是:
    selecte flag from c where flag='y';
    if (flag=='y') select name from a;
    else select name from b;
    将上面三个sql语句合并,写出单独的一个sql语句,让我可以用php直接调用。($str_sql = "你写的SQL语句";$result = mysql_db_query("pcb", $str_sql); )注意:表a,表b,表c,三个表之间没有任何的联系,我只是根据表c中的字段值来决定是读表a的name,还是表b的name.
      

  5.   

    上面有个地方写错是selecte flag from c where int=5; (这个数字在程序中是变量$i_number)
    不是selecte flag from c where flag='y';
      

  6.   

    前一句应该是selecte flag from c where id=5 吧
    后面一句也应该是select name from a where id=5 吧,除非你的a,b表只有一条记录
      

  7.   

    如果你的id是关联的
    select a.name from a,c where c.flag='y' and c.id=$num and a.id=c.id
    union 
    select b.name from b,c where c.flag='n' and c.id=$num and a.id=c.id如果你真的要取全表的name
    select a.name from a,c where c.flag='y' and c.id=$num 
    union 
    select b.name from b,c where c.flag='n' and c.id=$num 
      

  8.   

    c中有几个记录数?
     helloyou0(你好!) 的做法可以一用
      

  9.   

    不要管数据库的设计问题了。这是我们老板自己弄的。没办法。再差他也不承认。再重复下,你们看下我已经改过的详细说明,弄明白我的意思啊。
    表a,字段有id int, name varchar
    表b,字段有id int, name varchar
    表c,字段有id int auto_increment, flag varchar(实际中,flag的值只会是"y"或者"n")我的要求是:
    selecte flag from c where id=5;
    if (flag=='y') select * from a where id=3;
    else select * from b where id=3;
    将上面三个sql语句合并,写出单独的一个sql语句,让我可以用php直接调用。($str_sql = "你写的SQL语句";$result = mysql_db_query("pcb", $str_sql); )注意:表a,表b,表c,三个表之间没有任何的联系,没有任何的联系,没有id关联,没有关联,说了好多次没有联系了。三个是独立的表。就是我写的那三个语句,合并成一个语句,还没明白???最后的结果应该是select * from a where id=3或select * from b where id=3的一个数据集。helloyou0(你好!),写的那个不行,我说了没有关联,没有关联了。而且你用union连接,那和我说的意思完全相反。我就是表a和表b之间选择一个。用union完全和我意思相反。
      

  10.   

    selecte flag from c where id=5;
    if (flag=='y') select * from a where id=3;
    else select * from b where id=3;为什么第一句是id=5,后面两句变成id=3呢,id之间是个什么关系?
      

  11.   

    helloyou0(你好!) 那个地方随便是多少啦,在PHP里,可能是变量。三个表的ID之间没有关系。
      

  12.   

    if exist (select flag from a where flag='(select flag from c where id='y')')
     
       print ('存在a表中')
     else
       print(' 在b表中')
      

  13.   

    select a.name from a,c where c.flag='y' and c.id=$num and a.id=$numofab
    union 
    select b.name from b,c where c.flag='n' and c.id=$num and b.id=$numofab;这里虽然使用union,但是因为c.flag='y' and c.id=$num 和 c.flag='n' and c.id=$num 这两个条件是矛盾的,所以每次实际上只会取得两个表中的一个。
    我马上做个例子给你,就更清楚了。
      

  14.   

    你看看是不是符合你的要求,
    mysql版本是4.1, 手册上说4.0.0开始支持union,但是似乎4.0.x的有时候会有问题。
    mysql> desc a;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(6)      |      |     | 0       |       |
    | name  | varchar(10) |      |     |         |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)mysql> select * from a;
    +----+------+
    | id | name |
    +----+------+
    |  1 | aaa  |
    |  2 | bbb  |
    |  3 | ccc  |
    |  4 | ddd  |
    |  5 | eee  |
    |  6 | fff  |
    +----+------+
    6 rows in set (0.00 sec)mysql> desc b;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | id    | int(6)      |      |     | 0       |       |
    | name  | varchar(10) |      |     |         |       |
    +-------+-------------+------+-----+---------+-------+
    2 rows in set (0.00 sec)mysql> select * from b;
    +----+------+
    | id | name |
    +----+------+
    |  1 | 111  |
    |  2 | 222  |
    |  3 | 333  |
    |  4 | 444  |
    |  5 | 555  |
    |  6 | 666  |
    +----+------+
    6 rows in set (0.00 sec)mysql> desc c;
    +-------+---------+------+-----+---------+----------------+
    | Field | Type    | Null | Key | Default | Extra          |
    +-------+---------+------+-----+---------+----------------+
    | id    | int(6)  |      | PRI | NULL    | auto_increment |
    | flag  | char(1) |      |     | y       |                |
    +-------+---------+------+-----+---------+----------------+
    2 rows in set (0.00 sec)mysql> select * from c;
    +----+------+
    | id | flag |
    +----+------+
    |  1 | y    |
    |  2 | n    |
    |  3 | n    |
    |  4 | y    |
    |  5 | n    |
    +----+------+
    5 rows in set (0.01 sec)mysql> select a.name from a,c where c.flag='y' and c.id=5 and a.id=3
        -> union
        -> select b.name from b,c where c.flag='n' and c.id=5 and b.id=3;
    +------+
    | name |
    +------+
    | 333  |
    +------+
    1 row in set (0.01 sec)mysql> select a.name from a,c where c.flag='y' and c.id=4 and a.id=2
        -> union
        -> select b.name from b,c where c.flag='n' and c.id=4 and b.id=1;
    +------+
    | name |
    +------+
    | bbb  |
    +------+
    1 row in set (0.00 sec)mysql>