简体   繁体   English

Mysql查询价格比较

[英]Mysql query for price comparison

I am working on a price comparison program. 我正在进行价格比较计划。 I have three websites. 我有三个网站。 In my database I have three tables for the webshops. 在我的数据库中,我有三个网上商店表。 Each tables has a price and a product_id column. 每个表都有一个价格和一个product_id列。 I want to list all the product_id-s and see the prices in every webshop. 我想列出所有product_id-s并查看每个网店的价格。 The product id is unique, but not the same in all of the tables, as not all webshop has the same products. 产品ID是唯一的,但在所有表中都不相同,因为并非所有的网店都有相同的产品。 I am working with mysql, but I'm very new to databases. 我正在使用mysql,但我对数据库很新。 What query should I try? 我应该尝试什么查询?

I suggest you have a "master" product table, that lists all products, regardless of whether they are sold on all sites, or just one. 我建议您有一个“主”产品表,列出所有产品,无论它们是在所有网站上销售,还是只销售一个。 Then join from that to each of the website pricing tables. 然后从那里加入到每个网站定价表。 Try matching on product name. 尝试匹配产品名称。 In its simplest form, the query wold look like: 在最简单的形式中,查询看起来像:

select
  p.*,
  t1.price as site1_price,
  t2.price as site2_price,
  t3.price as site3_price
from product p
left join website1 t1 on t1.name = p.name
left join website2 t2 on t2.name = p.name
left join website2 t3 on t3.name = p.name;

You might have to try joining on brand and model, ie on t1.brand = p.brand and t1.model = p.model , or some other criteria if name is not unique. 您可能必须尝试加入品牌和型号,即on t1.brand = p.brand and t1.model = p.model ,或者如果名称不是唯一的,则可以尝试其他一些标准。

site prices will be null if they don't sell a product. 如果他们不销售产品,网站价格将为空。

To quickly populate product, you could run this: 要快速填充产品,您可以运行以下命令:

insert into product (name, brand, model, ...)
select name, brand, model, ... from website1
union 
select name, brand, model, ... from website2
union 
select name, brand, model, ... from website3;

FYI, the use of UNION (rather than UNION ALL ) makes the output of the union produce only unique rows 仅供参考,使用UNION (而不是UNION ALL )会使UNION ALL的输出仅产生唯一的行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM