PHP,mysql中,我想在多个表中查询一个字段
mysql中,我想在多个表中查询一个字段,表名分别是index_news,college_news,learning_news,party_news,social_new...
mysql中,我想在多个表中查询一个字段,表名分别是index_news,college_news,learning_news,party_news,social_news 这几个表中都有title字段。
我想在多个表中查询title,把title like '图'的新闻全查出来,求救啊,100分全给了 展开
我想在多个表中查询title,把title like '图'的新闻全查出来,求救啊,100分全给了 展开
3个回答
展开全部
前提所有表在一个数据库 不在一个数据库时 自己在变变
<?php
$arr=array("index_news","college_news","learning_news","party_news","social_news ");
$conn = @mysql_connect("localhost", "你的用户名", "你的密码") or die("数据库链接错误");
mysql_select_db("你的数据库名", $conn);
mysql_query("set names 'GBK'");
for($i=0;$i<5;$i++)
{
$sql="select * from $arr[$i] where title like '%图%'";
echo $sql;
$query=mysql_query($sql);
while($row=@mysql_fetch_array($query)){
echo $row[title];
}
}
?>
<?php
$arr=array("index_news","college_news","learning_news","party_news","social_news ");
$conn = @mysql_connect("localhost", "你的用户名", "你的密码") or die("数据库链接错误");
mysql_select_db("你的数据库名", $conn);
mysql_query("set names 'GBK'");
for($i=0;$i<5;$i++)
{
$sql="select * from $arr[$i] where title like '%图%'";
echo $sql;
$query=mysql_query($sql);
while($row=@mysql_fetch_array($query)){
echo $row[title];
}
}
?>
展开全部
是用Union关键词可以帮您。如果有重复的记录又不想被合并重复记录,则可以使用Union all来代替union。需要注意的是所有的 select 后面筛选的结果记录的列数必须一样(比如下面的筛选列数只有title一个)。
select title from index_news where title like '图'
union
select title from college_news where title like '图'
union
select title from learning_news where title like '图'
union
select title from party_news where title like '图'
union
select title from social_news where title like '图'
select title from index_news where title like '图'
union
select title from college_news where title like '图'
union
select title from learning_news where title like '图'
union
select title from party_news where title like '图'
union
select title from social_news where title like '图'
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把几句SQL用UNION连结不就行吗?
SELECT title * FROM index_news WHERE title LIKE '%图%'
UNION
SELECT title * FROM learning_news WHERE title LIKE '%图%'
UNION
SELECT title * FROM college_news WHERE title LIKE '%图%'
UNION
...
SELECT title * FROM index_news WHERE title LIKE '%图%'
UNION
SELECT title * FROM learning_news WHERE title LIKE '%图%'
UNION
SELECT title * FROM college_news WHERE title LIKE '%图%'
UNION
...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询