(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)
union
(select t2_f1 as f1,t2_f2 as f2 from table2 where t2_tiaojian1=true)
$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是不是要求数据结构必须一样啊?
select area_no as a,name as b from area
union
select bid as a,btitle as b from bcproj_x;
不过你的mysql要在5.0以上才支持。
如果用视图,不太现实吧?还有其它办法吗?
WHERE field1_index like '%$kw%' OR field2_index like '%$kw%'
$sql = "SELECT field1_index, field2_index FROM test_table WHERE field1_index like '%$kw%' OR field2_index like '%$kw%'";
$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";
$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
这个还不行吧!
-> union
-> (select user_id as user from pub_member where user_id=80);
+----------------------+
| user |
+----------------------+
| 一二三四五一二三四五 |
| 80 |
+----------------------+
2 rows in set (0.00 sec)
select bno as a,btitle as b from bcproj_x
两句话分别单独运行,看是否有错
比如 表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应该就可以了
上面的已经给了那么多的源码
可以查查mysql手册union的用法