- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想制作一个通知页面,显示有关各种事物的通知,例如新关注者、新喜欢、新评论等。我想显示一个列表,按时间顺序显示所有这些事物。
我的表格是这样的:
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...
}
我将有单独的 SQL 查询,就像上面的查询一样,每个查询都加载一些东西。上面的示例加载关注者,另一个查询将加载评论等。我想显示所有这些查询的结果并按时间顺序显示它们,如下所示:
@user_1 liked your photo
@user_4 started following you
@user_2 commented on your photo
etc...
我怎样才能做到这一点? SQL UNION 要求表具有相同的列数并且所选列必须具有相同的名称。我没有这些。此外,每种结果(关注者、评论或点赞)都会有不同的标记。关注者通知将有一个关注按钮,评论通知将有一个按钮可以重定向到被点赞的照片等。
最佳答案
SQL UNION requires the tables to have the same number of columns and the selected columns must have the same name.
不,它没有。这里的表“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 联合运算符只要求 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;
您可以对日期和 varchar 使用单个列,但这通常不是一个好主意。 (将日期与明显不是日期的内容混合通常不是一个好主意。)
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;
关于php - 合并多个 SQL 查询的结果(UNION 不可能,因为列名不同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27278551/
这个问题已经有答案了: When to use single quotes, double quotes, and backticks in MySQL (13 个答案) 已关闭 3 年前。 我正在尝
我需要一个返回某些列值的选择查询。我想要的列以“U_S”开头。 Select * from em 我需要转换上面的查询。 '*' 必须是以下结果(但带有逗号): select COLUMN_NAME
在JPA中设置一对多关系时如何设置外键的列名? 我想将“items_id”的名称更改为“item_id” @OneToMany private List items; 我尝试了以下注释但没有成功: @
我有一个jqGrid列定义如下 name : 'idmycolumn', index : 'idmycolumn', width : 80,
我是否可以编写一个带参数的存储过程,即 mysql 查询,存储过程返回查询的列名? 例如我调用程序: 调用选择器('select * from users') 过程返回列名。使用 informatio
嗨,我刚刚开始学习 sql,我希望使用 concat 将列合并为一个列,但没能做到。我可以在没有 concat 的情况下运行代码,但是当我使用 concat 时,它会给我一个错误代码。谁能告诉我我做错
我们正在使用 java jdk 1.7.0_45,postgresql jdbc 连接器 postgresql-9.3-1100.jdbc41.jar。 这是我们问题的概要,下面粘贴了尽可能多的代码。
import pandas as pd import numpy as np rng = pd.date_range('1/1/2011', periods=6, freq='H') df = pd.
借助 PHP 的 mysqli 扩展,我可以使用 fetch_field() 方法通过 orgname 和 获取列和表的原始(无别名)名称结果中的 orgtable。 PDO 提供了方法getColu
我在 php 中使用 PDO,因此无法使用准备好的语句转义表名或列名。以下是我自己实现它的万无一失的方法吗: $tn = str_replace('`', '', $_REQUEST['tn']);
我想使用 apply 来跨越矩阵的行,并且我想在我的函数中使用当前行的行名。好像不能用rownames , colnames , dimnames或 names直接在函数内部。我知道我可以根据 thi
在编写管理数据的应用程序时,允许最终用户创建或删除最好表示为列的数据类别通常很有用。例如,我正在开发字典构建应用程序;用户可能会决定他们想要向数据添加“备用拼写”字段或其他内容,这可以很容易地表示为另
在我的数据框中,许多列名称以“.y”结尾,如示例所示: dat <- data.frame(x1=sample(c(0:1)), id=sample(10), av1.y = sample(10) ,
在 SQL Server 中,我希望看到 Table_Name 以及数据库中与该 Table_Name 关联的所有列。所以输出应该是这样的: TABLE_NAME COLUMN_N
在我的数据框中,许多列名称以“.y”结尾,如示例所示: dat <- data.frame(x1=sample(c(0:1)), id=sample(10), av1.y = sample(10) ,
在一种情况下,我们动态创建 sql 到 create动态临时表。 table_name 没有问题,因为它是由我们决定的,但是列名是由我们无法控制的来源提供的。 通常我们会使用以下查询检查列名: sel
我有一个数据框,我们可以通过代理 df = pd.DataFrame({'a':[1,0,0], 'b':[0,1,0], 'c':[1,0,0], 'd':[2,3,4]}) 还有一个类别系列 ca
我需要编写一个用户定义的函数,当应用于数据框时,它将返回列位置、列名称、模式和每个变量的类。我能够创建一个返回模式和类的,但是当我包含位置/名称时,我总是收到错误。我一直在这样做, myFunctio
我刚开始使用 QueryDSL 并遇到了问题。是否可以使用列名进行 orderBy?我为 orderBy 动态路径生成找到了这个: Generic querydsl orderBy dynamic p
操作 DataGridView 单元格时,您通常会执行以下操作: MyGrid.CurrentRow.Cells["EmployeeFirstName"].Value = "John"; 这一切都很好
我是一名优秀的程序员,十分优秀!