为参数化视图

时间:2016-07-25 23:03:11

标签: mysql sql

  • Linux版本5.5.49,基于Linux Mint(基于Ubuntu 14.04.1)

人员表包含了所有家庭:杰德,大卫,莎拉和卢克。汽车表包含我们拥有的两辆汽车。 CarPeople表包含每个乘坐汽车的人的条目,并包含将personID与carID相关联的键。最后是carData表,其中包含汽车的颜色,最后涂漆汽车的人的personID,油漆作业的图像,油漆作业的日期以及查看图像的URL。

这是一个奇怪的例子,但这个想法是只有与特定车辆相关联的人才能在车上画画或骑车。我插入的示例数据有Jed(1)和David(2)在车1中一起骑行,而David(2),Luke(3)和Sarah(4)乘坐车2。

以下查询结果必须包含在视图中,其中传递的参数引用了personID:

显示CarID,ImageURL和所有使用PersonID的人,但仅针对每个CarID的最新DataEntryTime。

我的people_concat列没有输出正确的结果,但我可以稍后解决。

我的主要问题是我无法弄清楚如何为此视图中的每个carid输出max dataentrytime的数据行。我尝试过的更改导致只输出一行,我无法弄清楚如何执行此操作。

关键的测试查询是这样的:

select c.* from (select @p1:=2 p) parm , carRiderList c;

这是关于用户2 David的视图中的选择。而不是5行,只应输出两个,CarID 2的第一个,以及carID 1的第一个。

运行这些查询应该会产生一行,一行包含关联personID的最高dataentrytime:

select c.* from (select @p1:=1 p) parm , carRiderList c;
select c.* from (select @p1:=3 p) parm , carRiderList c;
select c.* from (select @p1:=4 p) parm , carRiderList c;

谢谢!(SQL代码如下)

/* People in the database */

create table People
(
PersonID int(11) not null unique auto_increment,
phoneNumber varchar(30) not null unique,
Name varchar(40) not null unique,
PRIMARY KEY (PersonID)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into People (phoneNumber, Name)
values ("123-456-7891", "Jed"), ("223-456-7891", "David"), ("323-456-7891", "Luke"), ("423-456-7891", "Sarah");
select * from People;

/* Cars */

drop table if exists Cars;
create table Cars
(
CarID int(11) not null unique auto_increment,
PurchaseTime date,
PRIMARY KEY (CarID)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into Cars (PurchaseTime)
values ("2016-1-1"),("2016-1-2");
select * from Cars;

/* CarPerson: Will contain an entry for each person who rides in the car, and can paint it */

drop table if exists CarPerson;
create table CarPerson
(
CarPersonID int(11) not null unique auto_increment,
CarID int(11) not null,
CarRiderID int(11) not null,
PRIMARY KEY (CarPersonID),
FOREIGN KEY (CarID) references Cars(CarID),
FOREIGN KEY (CarRiderID) references People(PersonID)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into CarPerson(CarID, CarRiderID)
values (1, 1), (1, 2), (2, 2), (2, 3), (2, 4);
select * from CarPerson;

/* CarData: Contains CarID, the color was that car was painted, and the person who painted it, as well as a (fake) imageurl */

create table carData
(
DataID int(11) not null unique auto_increment,
CarID int(11) not null,
Color varchar(20),
DataEntryTime date, 
PainterID int(11) not null,
ImageURL varchar(255),
PRIMARY KEY (DataID),
FOREIGN KEY (CarID) references Cars(CarID),
FOREIGN KEY (PainterID) references People(PersonID)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;

insert into carData(CarID, Color, DataEntryTime, PainterID, ImageURL)
values (1, "Blue", "2013-1-1", 1, "http://www.bluecivic.com"),
(1, "Green", "2013-1-2", 3, "http://greencivic.com"),
(2, "Red", "2014-1-3", 4, "http://redford.com"),
(2, "Purple", "2014-1-4", 2, "http://purpleford.com"),
(2, "Black","2014-1-5", 3, "http://blackford.com");
select * from carData;

/* View I am having trouble with */

create function p1() returns INTEGER DETERMINISTIC NO SQL return @p1;
drop view carRiderList;
create view carRiderList as
(select distinct cp.CarID, cd.ImageURL, DataEntryTime, (
            select group_concat(Name order by CarID, PersonID = p1())from People p
            left join CarPerson cp 
            on p.PersonID = cp.CarPersonID 
            where cp.CarID IN (
                select distinct(CarID) 
                from CarPerson cp
                where cp.CarPersonID = p1()
                limit 1
            )
        ) 
as people_concat
from CarPerson cp
left join carData cd
on cd.CarID = cp.CarID
where cp.CarRiderID = p1()) order by DataEntryTime desc;
select c.* from (select @p1:=2 p) parm , carRiderList c;

0 个答案:

没有答案