简体   繁体   English

合并多个SQL查询的结果(由于列名不同,因此无法使用UNION)

[英]Combine results of multiple SQL queries (UNION not possible because column names are different)

I want to make a notifications page which shows notifications about a variety of things, like new followers, new likes, new comments etc. I want to display a list that shows all of these things in chronological order. 我要创建一个通知页面,其中显示有关各种事物的通知,例如新关注者,新喜欢,新评论等。我想显示一个列表,按时间顺序显示所有这些事物。

My tables look like this: 我的表如下所示:

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

I would load the followers of a user with a query like this: 我会用这样的查询加载用户的关注者:

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;
}

Next, I get the results like this: 接下来,我得到这样的结果:

foreach($followers_result AS $followers_row) {
    $follower_user_id = $followers_row['follower__user__id'];
    // the rest of the variables will come here...
}

I will have separate SQL queries like the one above which each load something. 我将有单独的SQL查询,例如上面的查询,每个查询都加载一些东西。 The example above loads the followers, another query will load the comments etc. I want to display the results of all of these queries and display them in chronological order, like this: 上面的示例加载了关注者,另一个查询将加载评论等。我想显示所有这些查询的结果并按时间顺序显示,如下所示:

@user_1 liked your photo
@user_4 started following you
@user_2 commented on your photo
etc...

How can I achieve this? 我该如何实现? SQL UNION requires the tables to have the same number of columns and the selected columns must have the same name. SQL UNION要求表具有相同数量的列,并且所选列必须具有相同的名称。 I don't have all that. 我还没有 Moreover, every kind of result (follower, comment or like) will have different markups. 此外,每种结果(追随者,评论或类似内容)将具有不同的标记。 A follower notification will have a follow button, a comment notification will have a button that redirects to the photo that was liked etc. 关注者通知将具有关注按钮,评论通知将具有重定向至喜欢的照片等的按钮。

You can use UNION but you'll need to use 'AS' to give columns the same name. 您可以使用UNION,但需要使用'AS'为列赋予相同的名称。 You'll also need to add a line like this to each select: 您还需要向每个选择项添加这样的行:

, 'comment' as Type FROM comment

and: 和:

, 'follow' as Type FROM follow

SQL UNION requires the tables to have the same number of columns and the selected columns must have the same name. SQL UNION要求表具有相同数量的列,并且所选列必须具有相同的名称。

No, it doesn't. 不,不是。 Here table "a" has two columns, integer and varchar. 在此,表“ a”具有两列,整数和varchar。

create table a (
  a_id integer,
  a_desc varchar(10)
);
insert into a values (1, 'aaaaaaaa'), (2, 'bbbbbbbb');

Table "b" has three columns, varchar, date, and char. 表“ 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');

The SQL union operator only requires that the SELECT clauses (not tables) have the same number of columns, and that they be of compatible data types. SQL联合运算符仅要求SELECT子句(非表)具有相同数量的列,并且它们具有兼容的数据类型。 You can usually cast incompatible types to something more useful. 通常,您可以将不兼容的类型转换为更有用的内容。

-- 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;

You can use a single column for date and varchar, but it's usually not a good idea. 可以将一列用于date和varchar,但这通常不是一个好主意。 (Mixing dates with something that's clearly not a date is usually not a good idea.) (将日期与显然不是日期的内容混合通常不是一个好主意。)

select cast(a_id as char) as col_1, a_desc as col_2
from a
union all
select b_id, created_date 
from b;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM