A表10个字段
字段分别为aid, title, cid, content, atime, aorder, acount,a1, a2, mtimeB表9个字段
字段分别为bid, title, cid, content, btime, border, b3, b4, mtime现在我获取到一个关键词$kw,我需要同时搜索A表和B表的title字段,获取到所有like %$kw%的所有记录,并根据mtime来排序。获取的记录里,需要保留的字段有a(b)id,title,cid,content,mtime这几个。
$sql = 'SELECT ';
$sql .= 'A.aid as id,B.bid AS id,';
$sql .= 'A.title as title,B.title as title,';
$sql .= 'A.cid as cid,B.cid as cid,';
$sql .= 'A.content as content,B.content as content,';
$sql .= 'A.mtime as mtime,B.mtime as mtime';
$sql .= 'FROM A,B WHERE title like "%'.$kw.'%" ORDER BY mtime DESC';
我这样构造sql,可行吗?如果不可行,那么怎么写这个sql语句呢?
字段分别为aid, title, cid, content, atime, aorder, acount,a1, a2, mtimeB表9个字段
字段分别为bid, title, cid, content, btime, border, b3, b4, mtime现在我获取到一个关键词$kw,我需要同时搜索A表和B表的title字段,获取到所有like %$kw%的所有记录,并根据mtime来排序。获取的记录里,需要保留的字段有a(b)id,title,cid,content,mtime这几个。
$sql = 'SELECT ';
$sql .= 'A.aid as id,B.bid AS id,';
$sql .= 'A.title as title,B.title as title,';
$sql .= 'A.cid as cid,B.cid as cid,';
$sql .= 'A.content as content,B.content as content,';
$sql .= 'A.mtime as mtime,B.mtime as mtime';
$sql .= 'FROM A,B WHERE title like "%'.$kw.'%" ORDER BY mtime DESC';
我这样构造sql,可行吗?如果不可行,那么怎么写这个sql语句呢?
解决方案 »
- 对象元素名中含变量怎么办?
- 图片在文件夹中,图片的id 名称 上传时间写在数据库中,要怎么写代码才能在网页上显示图片??、
- 能否把FCKeditor里的按钮取到外面来?
- ruby干嘛用的?我看到有人拿它和php,jsp来比
- 提问: 关于HTML中嵌入PHP<?=expression?>不起作用
- 近段时间内多谢各位好心人相助,第二个小作品完成,希望各位多提意见,并赠送各位一个免费的PHP空间+MYSQL数据库
- mssql_result取数据库varchar的字段时为何有字数限制?
- 吐血求适合初学者的简单的开发实例和开发目录
- 为什么会出现cgi error?
- 大家有谁知道快狗如何制作的?
- 上传文件 move_uploaded_file的问题
- 怎么用PHP来过滤输入的内容中包含的元字符
union
select b.id, b.xxx from b where b.title like '%$km%'
order by mtime descunion allselect bid,title,cid,content,mtime from b where title like '%km%'
order by mtime desc
select aid as id, title, cid, content,mtime from a where title like '%kw%'union allselect bid as id, title, cid, content,mtime from b where title like '%kw%'order by mtime desc综合了两位的帮助,最后结果这样就可以了。