求一将mysql一数据库下多表循环合并查询语句?该怎么处理

WBOY
Release: 2016-06-13 10:11:02
Original
1000 people have browsed it

求一将mysql一数据库下多表循环合并查询语句?
$str="SELECT * FROM talbe having ( 姓名 like '%奥巴马%'||职业 like '%政治家%') limit ".($page-1)*$num."


PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->[email protected]_connect("localhost","root","123456","test");//连接test数据库后,如何将test下的各个表合并查询输出结果?貌似for(...){//循环求出表名....tableabcxyz= talbe1,talbe2,...talbe3}$str="SELECT * FROM talbeabcxyz having ( 职业 like  '%医生%') limit ".($page-1)*$num."....
Copy after login


求解。



------解决方案--------------------
每张表建立外键,可将所有表链接起来查询。
如:表1:
CREATE TABLE `mis_link` (
`lid` int(6) NOT NULL,
`cid` int(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
表2:
CREATE TABLE `mis_commodity` (
`cid` int(6) NOT NULL auto_increment,
`comName` varchar(20) NOT NULL,
`comPrice` varchar(20) NOT NULL,
PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
表3:
CREATE TABLE `mis_order` (
`oid` int(6) NOT NULL auto_increment,
`sid` int(6) NOT NULL,
`cid` int(6) NOT NULL,
`ordTime` varchar(20) NOT NULL default '',
`ordPrice` varchar(20) NOT NULL default '',
PRIMARY KEY (`oid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

SQL code
select o.oid,l.lid,c.cid,comName,comPrice,ordTimefrom mis_order o,mis_link l,mis_commodity cwhere o.oid=l.lid and c.cid=l.cid;<br><font color="#e78608">------解决方案--------------------</font><br>talbe 就没懂啥意思...table就有点明白了...<br><br>select name from table1 where name like "obama%"<br>union<br>select name from table2 where name like "obama%"<br><br><br>尝试使用union操作,但注意select出来的列数必须一致,不一致的可以用null补充<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨
可以改成 以下吗?
1. [表名2名称的缩写]改成 直接用 [表名2];
2. 去掉form后面的 as和缩写名称;为什么要加as和缩写名称呢?
3.where后是我想要的条件:
如:

select [表名1].表1字段,[表名2].表2字段,[表名3].表3字段,……
from [表1名], [表2名], ……
having ( 职业 like '%医生%'||其他 like '%医生%') limit ".($page-1)*$num.",".$num;
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template