(select t1_f1 as f1,t1_f2 as f2 from table1 where t1_tiaojian1=true)
  union 
(select t2_f1 as f1,t2_f2 as f2 from table2 where t2_tiaojian1=true)

解决方案 »

  1.   

    要分面的话对union 后的结果limit 
      

  2.   

    假如我的SQL语句是
    $sqla="(select area_no as a,name as b from area) 
      union 
    (select bid as a,btitle as b from bcproj_x)";
    $resulta=mysql_query($sqla);
    while($a=mysql_fetch_array($resulta))
    {
    print_r($a);
    }
    先不考虑那个模糊查询,运行后的结果是
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\web\02.php on line 56
    union是不是要求数据结构必须一样啊?
      

  3.   

    用视图?在PHP中怎么用啊?这几个表之间没有任何关系的。
      

  4.   

    CREATE VIEW t2_t3 AS 
    select area_no as a,name as b from area
      union
    select bid as a,btitle as b from bcproj_x; 
    不过你的mysql要在5.0以上才支持。
      

  5.   

    现在情况是这样的:有一个表单来提交要搜索的关键字,从表TB1,TB2,TB3....中的每个字段中查找,如果存在这个关键字就读取这条记录
    如果用视图,不太现实吧?还有其它办法吗?
      

  6.   

    $sql = SELECT field1_index, field2_index FROM test_table
    WHERE field1_index like '%$kw%' OR  field2_index like '%$kw%'
      

  7.   

    修改一下:
    $sql = "SELECT field1_index, field2_index FROM test_table WHERE field1_index like '%$kw%' OR  field2_index like '%$kw%'";
      

  8.   

    field1_index, field2_index ?test_table ?这是在一张表中查询吗?解释一下吧........
      

  9.   


    $sql3="(select id,stuid,uptime,inforinfor_title,inforinfor_man,inforinfor_manto from stuinfor where inforinfor_manto=".$_SESSION["memchecked"]." and inforinfor_answer=1) union all (select id,eduid,uptime,inforinfor_title,inforinfor_man,inforinfor_manto from eduinfor where inforinfor_manto=".$_SESSION["memchecked"]." and inforinfor_answer=1) order by id desc";
      

  10.   

    表结构不同,没有关系,只要你前面select语句和后面的select语句,选出的字段数量相同就可以
      

  11.   

    那么我用你这种方法代码如下:
    $sqla="(select area_no as a,name as b from area) union all (select bno as a,btitle as b from bcproj_x)";
    $resulta=mysql_query($sqla);
    while($a=mysql_fetch_array($resulta))
    {print_r($a);}
    运行后的结果出现问题Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\web\02.php on line 54
    这个还不行吧!
      

  12.   

    mysql> (select nickname as user from userinfo where id=80 )
        -> union
        -> (select user_id as user from pub_member where user_id=80);
    +----------------------+
    | user                 |
    +----------------------+
    | 一二三四五一二三四五 |
    | 80                   |
    +----------------------+
    2 rows in set (0.00 sec)
      

  13.   

    select area_no as a,name as b from area 和 
    select bno as a,btitle as b from bcproj_x
     两句话分别单独运行,看是否有错
      

  14.   

    还有一个可能,mysql 4.0 之前的版本不支持union ,你不会是3.23的吧
      

  15.   

    哎!我的MYSQL是4.0的,那么有什么办法可以实现呢?
      

  16.   

    从4.0开始就已经支持union了,但可能由于多个表中对应的字段类型,长度之类的会出点问题。对于字段个数不一样,可以用拼凑的办法写。
    比如 表a中有三个字段(id, name, pass)
         表b中有两个字段(id, class)可以这样写:
     SELECT id as num, name as name, pass as pass from table_a
     UNION
     SELECT id as num, class as name, concat('') as pass from table_b
     ORDER BY num desc
     LIMIT.... 表多的话在union应该就可以了
      

  17.   

    使用union吧
    上面的已经给了那么多的源码
    可以查查mysql手册union的用法
      

  18.   

    正要做这个,用union应该行了