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

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

WBOY
WBOYOriginal
2016-06-13 10:11:02954Durchsuche

求一将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."....


求解。



------解决方案--------------------
每张表建立外键,可将所有表链接起来查询。
如:表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>
探讨
可以改成 以下吗?
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;
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn