Php 合并多个SQL查询的结果(由于列名不同,因此无法合并)
我想做一个通知页面,显示关于各种事情的通知,比如新关注者、新喜欢、新评论等。我想显示一个列表,按时间顺序显示所有这些事情 我的桌子是这样的:Php 合并多个SQL查询的结果(由于列名不同,因此无法合并),php,mysql,Php,Mysql,我想做一个通知页面,显示关于各种事情的通知,比如新关注者、新喜欢、新评论等。我想显示一个列表,按时间顺序显示所有这些事情 我的桌子是这样的: COMMENT 1 comment__id 2 comment__user_id 3 comment__snap__id 4 comment__text 5 comment_add_time LIKE 1 like__id 2 like__user__id 3 like__snap__id 4 like__like_t
COMMENT
1 comment__id
2 comment__user_id
3 comment__snap__id
4 comment__text
5 comment_add_time
LIKE
1 like__id
2 like__user__id
3 like__snap__id
4 like__like_time
FOLLOW
1 follow__id
2 follower__user__id
3 followed__user__id
4 follow__follow_time
5 follow__request_status
try {
$select_followers_query = '
SELECT follow.follower__user__id, follow.followed__user__id, follow.follow__request_status, user.user__id, user.user__username, user.user__profile_picture, user.privacy
FROM follow
JOIN user ON(follow.follower__user__id = user.user__id)
WHERE followed__user__id = :followed__user__id';
$prep_select_followers = $conn->prepare($select_followers_query);
$prep_select_followers->bindParam(':followed__user__id', $get_user__id, PDO::PARAM_INT);
$prep_select_followers->execute();
$followers_result = $prep_select_followers->fetchAll();
$followers_count = count($followers_result);
}
catch(PDOException $e) {
$conn = null;
echo $error;
}
foreach($followers_result AS $followers_row) {
$follower_user_id = $followers_row['follower__user__id'];
// the rest of the variables will come here...
}
@user_1 liked your photo
@user_4 started following you
@user_2 commented on your photo
etc...
我会用如下查询加载用户的追随者:
COMMENT
1 comment__id
2 comment__user_id
3 comment__snap__id
4 comment__text
5 comment_add_time
LIKE
1 like__id
2 like__user__id
3 like__snap__id
4 like__like_time
FOLLOW
1 follow__id
2 follower__user__id
3 followed__user__id
4 follow__follow_time
5 follow__request_status
try {
$select_followers_query = '
SELECT follow.follower__user__id, follow.followed__user__id, follow.follow__request_status, user.user__id, user.user__username, user.user__profile_picture, user.privacy
FROM follow
JOIN user ON(follow.follower__user__id = user.user__id)
WHERE followed__user__id = :followed__user__id';
$prep_select_followers = $conn->prepare($select_followers_query);
$prep_select_followers->bindParam(':followed__user__id', $get_user__id, PDO::PARAM_INT);
$prep_select_followers->execute();
$followers_result = $prep_select_followers->fetchAll();
$followers_count = count($followers_result);
}
catch(PDOException $e) {
$conn = null;
echo $error;
}
foreach($followers_result AS $followers_row) {
$follower_user_id = $followers_row['follower__user__id'];
// the rest of the variables will come here...
}
@user_1 liked your photo
@user_4 started following you
@user_2 commented on your photo
etc...
接下来,我得到如下结果:
COMMENT
1 comment__id
2 comment__user_id
3 comment__snap__id
4 comment__text
5 comment_add_time
LIKE
1 like__id
2 like__user__id
3 like__snap__id
4 like__like_time
FOLLOW
1 follow__id
2 follower__user__id
3 followed__user__id
4 follow__follow_time
5 follow__request_status
try {
$select_followers_query = '
SELECT follow.follower__user__id, follow.followed__user__id, follow.follow__request_status, user.user__id, user.user__username, user.user__profile_picture, user.privacy
FROM follow
JOIN user ON(follow.follower__user__id = user.user__id)
WHERE followed__user__id = :followed__user__id';
$prep_select_followers = $conn->prepare($select_followers_query);
$prep_select_followers->bindParam(':followed__user__id', $get_user__id, PDO::PARAM_INT);
$prep_select_followers->execute();
$followers_result = $prep_select_followers->fetchAll();
$followers_count = count($followers_result);
}
catch(PDOException $e) {
$conn = null;
echo $error;
}
foreach($followers_result AS $followers_row) {
$follower_user_id = $followers_row['follower__user__id'];
// the rest of the variables will come here...
}
@user_1 liked your photo
@user_4 started following you
@user_2 commented on your photo
etc...
我将有单独的SQL查询,就像上面的查询一样,每个查询都会加载一些内容。上面的示例加载追随者,另一个查询将加载评论等。我想显示所有这些查询的结果,并按时间顺序显示它们,如下所示:
COMMENT
1 comment__id
2 comment__user_id
3 comment__snap__id
4 comment__text
5 comment_add_time
LIKE
1 like__id
2 like__user__id
3 like__snap__id
4 like__like_time
FOLLOW
1 follow__id
2 follower__user__id
3 followed__user__id
4 follow__follow_time
5 follow__request_status
try {
$select_followers_query = '
SELECT follow.follower__user__id, follow.followed__user__id, follow.follow__request_status, user.user__id, user.user__username, user.user__profile_picture, user.privacy
FROM follow
JOIN user ON(follow.follower__user__id = user.user__id)
WHERE followed__user__id = :followed__user__id';
$prep_select_followers = $conn->prepare($select_followers_query);
$prep_select_followers->bindParam(':followed__user__id', $get_user__id, PDO::PARAM_INT);
$prep_select_followers->execute();
$followers_result = $prep_select_followers->fetchAll();
$followers_count = count($followers_result);
}
catch(PDOException $e) {
$conn = null;
echo $error;
}
foreach($followers_result AS $followers_row) {
$follower_user_id = $followers_row['follower__user__id'];
// the rest of the variables will come here...
}
@user_1 liked your photo
@user_4 started following you
@user_2 commented on your photo
etc...
我怎样才能做到这一点?SQL UNION要求表具有相同的列数,并且所选列必须具有相同的名称。我没有这些。此外,每种结果(关注者、评论等)都会有不同的标记。关注者通知将有一个follow按钮,注释通知将有一个重定向到喜欢的照片的按钮等。您可以使用UNION,但需要使用“AS”为列指定相同的名称。 您还需要在每个选择中添加一行,如下所示:
, 'comment' as Type FROM comment
以及:
, 'follow' as Type FROM follow
SQL UNION要求表具有相同的列数,并且所选列必须具有相同的名称
不,没有。表“a”有两列,integer和varchar
create table a (
a_id integer,
a_desc varchar(10)
);
insert into a values (1, 'aaaaaaaa'), (2, 'bbbbbbbb');
表“b”有三列,varchar、date和char
create table b (
b_id varchar(10),
created_date date,
unused char(1)
);
insert into b values ('xyz', '2014-01-01', 'x'), ('tuv', '2014-01-13', 'x');
SQL union运算符只要求SELECT子句(而不是表)具有相同的列数,并且它们具有兼容的数据类型。您通常可以将不兼容的类型转换为更有用的类型
-- SELECT clause has three columns, but table "a" has only two.
-- The cast is for illustration; MySQL can union an integer with a
-- varchar without a cast.
--
select cast(a_id as char) as col_1, a_desc as col_2, null as col_3
from a
union all
-- Note that these columns don't have the same names as the columns
-- above.
select b_id, null, created_date
from b;
可以使用单个列来表示date和varchar,但这通常不是一个好主意。(把日期和显然不是日期的东西混在一起通常不是个好主意。)
“SQL UNION要求表具有相同的列数,并且所选列必须具有相同的名称。”您错了。有几种方法可以解决工会的表格和列要求。“你错了。有几种方法可以解决工会的表格和列要求”-启发我。我找不到这方面的任何信息………您需要使用“AS”为列指定相同的名称“不,您没有。谢谢您的详细解释!