我在sql,person和status中有两个表。
我想按人名搜索一行,获取行后获取id,然后使用该ID获取其他表的其他行
我有
DROP TABLE IF EXISTS `tbl_person`;
CREATE TABLE `tbl_person` (
id_person INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(25) NOT NULL ,
email VARCHAR(25) NOT NULL ,
info VARCHAR(25) NOT NULL ,
info2 VARCHAR(25) NOT NULL ,
CONSTRAINT `uc_Info_Person` UNIQUE (`id_person`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
DROP TABLE IF EXISTS `tbl_status` ;
CREATE TABLE `tbl_status` (
id_status INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
id_person INTEGER NOT NULL,
a_ingre INTEGER NOT NULL DEFAULT 0,
b_on_alfa INTEGER NOT NULL DEFAULT 0,
c_on_bith INTEGER NOT NULL DEFAULT 0,
treatment VARCHAR(25) NOT NULL ,
medic VARCHAR(25) NOT NULL ,
reingr VARCHAR(25) NOT NULL ,
FOREIGN KEY (id_person) REFERENCES `tbl_person` (id_person),
CONSTRAINT `uc_Info_status` UNIQUE (`id_person`,`id_status`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
INSERT INTO `tbl_person` VALUES (1, 'JOE DOE', 'joe@doe.com','description','other one');
INSERT INTO `tbl_status` VALUES (1, 1, 10,20,30,'diclofenaco','dosis', 'reign');
和查询:
select * from `tbl_person` person
INNER JOIN `tbl_status` status
on person.name like 'Joe'
and person.id_person = status.id_person ;
但这并不能归还任何东西。是否可以制作涉及所有这一切的单个查询?
请查看the fiddle
答案 0 :(得分:2)
您在LIKE
操作数中错过了通配符(请参阅here)。以下将找到名称以" Joe"开头的记录,例如:
SELECT * FROM `tbl_person` person
INNER JOIN `tbl_status` status
ON person.name LIKE 'Joe%' -- Note the % trailing the search string
AND person.id_person = status.id_person;