我有两个独立的表:“客户”和“国家/地区”。
国家/地区表:
IdCountry Country
1 SPAIN
2 PORTUGAL
客户表
IdClient Entity IdCountry
1 Adam Alves 2
2 Peter Smith 2
3 David Ramos 1
4 Rafael Castro 1
我想在“客户”表中添加一个新客户,但使用“国家”表中的信息,如下所示:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT max(IdClient) + 1, '--New--' FROM Clients,
SELECT IdCountry FROM Country WHERE Country = 'SPAIN'
我想要这个输入:
IdClient Entity IdCountry
5 --New-- 1
但是,如果我运行此查询,它将无法正常工作。有人可以帮我吗?
评论:我更喜欢不使用自动增量选项。
非常感谢您。
Wardiam
答案 0 :(得分:0)
您可以这样做:
INSERT INTO Clients(IdClient, Entity, Country)
SELECT
(SELECT MAX(IdClient) + 1 FROM Clients),
'--New--',
(SELECT IdCountry FROM Country WHERE Country = 'SPAIN')
请参见demo。
结果:
| IdClient | Entity | Country |
| -------- | ------------- | ------- |
| 1 | Adam Alves | 2 |
| 2 | Peter Smith | 2 |
| 3 | David Ramos | 1 |
| 4 | Rafael Castro | 1 |
| 5 | --New-- | 1 |