我希望为服务数据库添加防水服务。我想为每个设备添加一次服务,不管它是否是店内服务。这是我目前的情况:
INSERT INTO services (serviceID, manufacturer, deviceName, serviceName, price, upc)
SELECT
DEFAULT,
manufacturer,
DISTINCT deviceName,
'Waterproofing Stand-alone',
'59.99',
'00813583026318'
FROM services
WHERE deviceName NOT LIKE '%(In-store)%';
请告诉我这是否会产生预期效果。
由于
答案 0 :(得分:2)
这不是多么有效。 Distinct返回所有选定列的值的唯一组合。
假设serviceId是一个自动递增列,您可以使用聚合并在其他列上使用max(或min)来为每个deviceName只获取一行。
insert into services (
manufacturer,
deviceName,
serviceName,
price,
upc
)
select max(manufacturer),
deviceName,
'Waterproofing Stand-alone',
'59.99',
'00813583026318'
from services
where deviceName not like '%(In-store)%'
group by deviceName;
如果每个deviceName只能有一个制造商,那么您不需要分组。只需DISTINCT
即可:
insert into services (
manufacturer,
deviceName,
serviceName,
price,
upc
)
select distinct manufacturer,
deviceName,
'Waterproofing Stand-alone',
'59.99',
'00813583026318'
from services
where deviceName not like '%(In-store)%';