求一将mysql一数据库下多表循环合并查询语句?该怎么处理
程序员文章站
2024-01-02 15:22:46
...
求一将mysql一数据库下多表循环合并查询语句?
$str="SELECT * FROM talbe having ( 姓名 like '%奥巴马%'||职业 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;
$str="SELECT * FROM talbe having ( 姓名 like '%奥巴马%'||职业 like '%政治家%') limit ".($page-1)*$num."
- PHP code
[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;
------解决方案--------------------
talbe 就没懂啥意思...table就有点明白了...
select name from table1 where name like "obama%"
union
select name from table2 where name like "obama%"
尝试使用union操作,但注意select出来的列数必须一致,不一致的可以用null补充
------解决方案--------------------
相关文章
相关视频