我有两张桌子。一个用于销售人员,另一个用于销售我希望通过ALPHA排序推销员的名称来命令查询的输出。
这是表结构:
CREATE TABLE `Salesman` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(45) NULL ,
PRIMARY KEY (`ID`) );
INSERT INTO `Salesman` (`Name`) VALUES ('Bill');
INSERT INTO `Salesman` (`Name`) VALUES ('John');
INSERT INTO `Salesman` (`Name`) VALUES ('Dave');
INSERT INTO `Salesman` (`Name`) VALUES ('Mark');
CREATE TABLE `Sales` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`Item` INT VARCHAR(45) NULL ,
`Salemesman_ID` INT NULL ,
`Total` INT NUll ,
PRIMARY KEY (`ID`) );
INSERT INTO `Sales` (`Item`, `Salemesman_ID`, `Total`) VALUES ('Pen', '3', '14');
INSERT INTO `Sales` (`Item`, `Salemesman_ID`, `Total`) VALUES ('Rat', '1', '12');
INSERT INTO `Sales` (`Item`, `Salemesman_ID`, `Total`) VALUES ('Car', '2', '1230');
INSERT INTO `Sales` (`Item`, `Salemesman_ID`, `Total`) VALUES('Rabbit', '2', '11');
INSERT INTO `Sales` (`Item`, `Salemesman_ID`, `Total`) VALUES('Towel', '1', '6');
INSERT INTO `Sales` (`Item`, `Salemesman_ID`, `Total`) VALUES('VaporWare', '4','900');
因为Alpha命令中的销售员名称是:Bill,Dave,John,Mark
我需要输出
Rat 1 12 (Bill = 1)
Towel 1 6 (Bill = 1)
Pen 3 14 (Dave = 3)
Car 2 1230 (John = 2)
Rabbit 2 11 (John = 2)
VaporWare 4 900 (Mark = 4)
答案 0 :(得分:4)
您可以通过一起加入表格来实现您想要的目标:
select s.*
from sales s join
SalesMan sm
on s.Salesman_Id = sm.Id
order by sm.name, sm.id;
我觉得有必要补充说,表名“推销员”似乎对人类的一半只有不必要的限制。