我想加入2个表。一个包含用户和其他约会 - 约会表包含3列(ApID,这是主键),bookFrom,bookedFor-这些最后2列引用users表,我认为这很容易self_explanatory他们的意思。 以下是详细表格:
CREATE TABLE `users` (
`user_ID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`lastname` varchar(45) NOT NULL,
`email` varchar(255) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`hash` varchar(32) DEFAULT NULL,
`usertype` enum('1','2','3','4') DEFAULT NULL,
PRIMARY KEY (`user_ID`),
KEY `fk_users_usertype1_idx` (`usertype`),
CONSTRAINT `fk_users_usertype1` FOREIGN KEY (`usertype`) REFERENCES `usertype` (`type_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8
CREATE TABLE `appointments` (
`apID` int(11) NOT NULL AUTO_INCREMENT,
`Bookfrom` int(11) ,
`bookedfor` int(11) ,
PRIMARY KEY (`apID`),
KEY `fk_appointments_user1_idx` (`Bookfrom`),
KEY `bokkedfor` (`bookedfor`),
CONSTRAINT `appointments_ibfk_3` FOREIGN KEY (`bookedfor`) REFERENCES `users` (`user_ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `appointments_ibfk_2` FOREIGN KEY (`Bookfrom`) REFERENCES `users` (`user_ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
我想要找到的是,如果某个特定用户(该用户的电子邮件为papageorgiou40@testmail.com)列在约会表中(意思是预约了约会),我正在尝试使用此声明执行此操作:
select appointments.Bookfrom,appointments.Bookedfor
from appointments,users
where users.email='papageorgiou40@test.com'
and appointments.Bookfrom=users.user_ID,
appointments.Bookedfor=users.user_ID;
但我收到以下消息:
错误代码1064,SQL状态42000:您的SQL中有错误 句法;查看与MySQL服务器版本对应的手册 正确的语法使用附近' appointmentments.Bookedfor = users.user_ID限制20 OFFSET 0'在第4行
你可能想知道为什么2列预订和预订。原因是在用户表中有2种类型的用户,业务用户和普通用户,因此逻辑是为一个业务预约的常规用户。我不确定这是适当的数据库设计,但是那个是一个不同的讨论
答案 0 :(得分:0)
我猜你想要OR
。或者你可以这样做:
... AND users.user_ID IN (appointments.Bookfrom,appointments.Bookedfor)
答案 1 :(得分:0)
您在where子句中缺少运算符:
select appointments.Bookfrom,appointments.Bookedfor
from appointments,users
where users.email='papageorgiou40@test.com'
and appointments.Bookfrom=users.user_ID,
appointments.Bookedfor=users.user_ID;
请改为尝试:
select appointments.Bookfrom,appointments.Bookedfor
from appointments,users
where users.email='papageorgiou40@test.com'
and (appointments.Bookfrom=users.user_ID
or appointments.Bookedfor=users.user_ID);