我创建了一个带有外键值的MySQL表。我想使用MySQL join来获取外键值。
我有一个名为employee
和外键值sex
的表,性别表包含男性&女。
这是我的简单连接查询:
SELECT * FROM sex JOIN employee ON employee.sex_id=sex.id
但我想在这里使用连接查询,但似乎我遗漏了一些东西,请完成此操作:
SELECT employee_id, first_name, last_name, sex_id FROM employee
请告诉我如何在此表的单个查询中insert to
forigen key:
CREATE TABLE IF NOT EXISTS `employee` (
`employee_id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) NOT NULL,
`middle_name` varchar(20) DEFAULT NULL,
`last_name` varchar(20) NOT NULL,
`employee_employee_id` int(11) DEFAULT NULL,
`address_id` int(11) DEFAULT NULL,
`phone_id` int(11) DEFAULT NULL,
`dob` date DEFAULT NULL,
`maritial_id` int(11) NOT NULL,
`sex_id` int(11) NOT NULL,
PRIMARY KEY (`employee_id`),
KEY `fk_employee_employee` (`employee_employee_id`),
KEY `fk_employee_address1` (`address_id`),
KEY `fk_employee_phone1` (`phone_id`),
KEY `fk_employee_maritial1` (`maritial_id`),
KEY `fk_employee_sex1` (`sex_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
我在MySQL中没有好主意
答案 0 :(得分:0)
尝试类似:
SELECT e.employee_id, e.first_name, e.last_name, s.id
FROM employee AS e
JOIN sex AS s
ON e.sex_id = s.id
答案 1 :(得分:0)
SELECT emp.employee_id, emp.first_name, emp.last_name, emp.sex_id, sx.sex_label
FROM employee AS emp
JOIN sex AS sx
ON emp.sex_id = sx.id
sex_label是{{1}}表中包含男性或女性的字段名称。