MySQL Workbench 中的 VIEWS 总是返回 (0) 行
Posted
技术标签:
【中文标题】MySQL Workbench 中的 VIEWS 总是返回 (0) 行【英文标题】:VIEWS in MySQL Workbench always return (0) row(s) 【发布时间】:2017-03-04 19:16:23 【问题描述】:drop database if exists RentaHouse;
create database RentaHouse;
use RentaHouse;
create table Staff(
staffNo char(5) not null primary key,
fName varchar(15),
lName varchar(15),
position varchar(15),
dob date,
salary decimal (7,2) unsigned
);
create table PropertyForRent(
propertyNo char(5) primary key,
street varchar(35) not null,
city varchar(15) not null,
pcode varchar(10),
type varchar(20) not null,
rooms tinyint unsigned not null,
rent decimal (6,2) unsigned,
staffNo char(5)
);
ALTER TABLE PropertyForRent
ADD CONSTRAINT whatever foreign key(staffNo) references Staff (staffNo) ON DELETE CASCADE ON UPDATE CASCADE;
insert into staff values
('s1234','Mary','Jones', 'Sales', '1975-12-22',45000),
('s1834','Pat','Roche', 'IT', '1972-09-13',42000),
('s1998','Michael','Brown', 'Sales', '1980-12-09',43500);
insert into propertyForRent values
('p3296','21 Ash Street','Tramore','WD34-543', 'Bungalow',4,1200,'s1234'),
('p3299','William Street','Dungarvan','WD99-088', 'Terrace',3,1050,'s1234'),
('p3344','9 Mary Street','New Ross','WX99-044', 'Terrace',3,800,'s1998'),
('p3356','21 Mary Street','New Ross','WX99-076', '2 Storey',4,1100,null);
/*doesn't work!*/
CREATE VIEW anyView AS
select * from Staff;
/*work!*/
select * from Staff;
/*work!*/
select street, city, type, rent, concat(fName, lName) as 'Name' from
PropertyForRent join Staff on PropertyForRent.staffNo = Staff.staffNo where city = 'New Ross' order by rent;
/*doesn't work!*/
CREATE VIEW myView AS
select street, city, type, rent, concat(fName, lName) as 'Name' from
PropertyForRent join Staff on PropertyForRent.staffNo = Staff.staffNo where city = 'New Ross' order by rent;
所有视图都没有返回任何结果! 我在 mysql Workbench 6.3.8 中使用“sakila”作为默认模式。
我已经在网上搜索了 2 天的解决方案,但我认为是时候问问谁有专业知识了。
P.S 视图不适用于我为这个架构创建的任何数据库!
【问题讨论】:
你试过用mysql命令行吗?尝试使用架构(从 RentaHouse.anyView 中选择 *;)。我完全没有发现任何问题。看一看:sqlfiddle.com/#!9/8e9ca1/2 问题出在视图中,不在选择语句中 sqlfiddle.com 不接受 VIEW。从 RentaHouse.anyView 中选择 *;作品 从 RentaHouse.anyView 中选择 *;如果它有效,则问题是您的工作台正在另一个数据库中创建视图。尝试通过单击 RentaHouse 数据库并将其设置为默认数据库来设置默认数据库。默认数据库应以粗体显示。 您创建了多个同名视图。第二件事是您还没有编写选择查询来显示视图数据。 【参考方案1】:CREATE VIEW anyView1 AS select * from Staff;
then fetch data
select * from anyView1;
CREATE VIEW myViewss AS
select street, city, type, rent, concat(fName, lName) as 'Name' from
PropertyForRent join Staff on PropertyForRent.staffNo = Staff.staffNo where city = 'New Ross' order by rent;
select * from myViewss;
【讨论】:
以上是关于MySQL Workbench 中的 VIEWS 总是返回 (0) 行的主要内容,如果未能解决你的问题,请参考以下文章
java中的简单mysql选择比MySQL Workbench中的慢