简体   繁体   English

MYSQL查询将列名匹配到另一个表中的行

[英]MYSQL Query matching column names to rows in another table

I have two tables in MYSQL, one contain a list of options a customer can pick from and the other has the cost of the options. 我在MYSQL中有两个表,一个包含客户可以选择的选项列表,另一个包含选项的成本。 I would like a view that returns the cost of each customers option set. 我想要一个返回每个客户选项集成本的视图。 For my example I'll use the analogy of buying a new car. 在我的示例中,我将使用购买新车的类比。

Customers table 客户表

customer - sunroof - mag_wheels - spoiler 
--------------------------------------------------
John     - true    - true       - false 
Steve    - false   - true       - false
Lucy     - false   - false      - false

Options table 选项表

option      - price
-----------------
sunroof     - 100
mag_wheels  - 150
spoiler     -  75

Desired results 所需结果

customer   - cost
-----------------
John       - 250
Steve      - 150
Lucy       - 0

or this would do, as I good easily multiply selected by price and then group by customer 或那样做,因为我很容易将选择的价格乘以然后按客户分组

customer   - option     - selected - price
------------------------------------------
John       - sunroof    - true     - 100
John       - mag_wheels - true     - 150
John       - spoiler    - false    -  75
Steve      - sunroof    - false    - 100
Steve      - mag_wheels - true     - 150
Steve      - spoiler    - false    -  75
Lucy       - sunroof    - false    - 100
Lucy       - mag_wheels - false    - 150
Lucy       - spoiler    - false    -  75

I've been puzzling over this for hours now and I can't even figure out where to start, a join seems out of the question as there are no common elements to match. 我已经花了几个小时对此感到困惑,而且我什至不知道从哪里开始,因为没有通用的元素可以匹配,所以加入似乎是不可能的。 I wonder if using UNION is the answer but I can't figure out how to combine row values with column headings. 我想知道是否使用UNION是答案,但是我不知道如何将行值与列标题结合起来。

If anyone could point me in the right direction I'd be ever so grateful, double points if you come up with a solution that dynamically picks up the different options so I could add more in the future without rewriting the query. 如果有人能向我指出正确的方向,我将不胜感激,如果您想出一种可以动态选择不同选项的解决方案,则可以加倍加分,这样以后我就可以在不重写查询的情况下添加更多内容。

Many thanks in advance. 提前谢谢了。

The reason I wanted to have all the options as a single row is I was hoping to use Access to make a form for picking the options and I couldn't figure out how a single form could create multiple rows. 我希望将所有选项都包含在一行中的原因是,我希望使用Access来创建用于选择选项的表单,但我无法弄清楚单个表单如何创建多行。

This is a horrible data layout. 这是一个可怕的数据布局。 You should have an association table, with one row per customer and option. 您应该有一个关联表,每个客户和一个选项一行。

But, you can do it: 但是,您可以这样做:

select c.customer, sum(o.cost) as cost
from customers c left outer join
     options o
     on (c.sunroof = true and o.option = 'sunroof' or
         c.mag_wheels = true and o.option = 'mag_wheels' or
         c.spoiler = true and o.option = 'spoiler'
        )
group by c.customer;

EDIT: 编辑:

You do not want all options in a single record. 希望所有选项都在一个记录中。 Instead, you need an association table: 相反,您需要一个关联表:

create table customer_options (
    customer_optionid unsigned auto_increment,
    customer varchar(255) references customer(name),
    option varchar(255) references option(option)
);

Actually you should really have integer primary keys for all the tables, and use them for the foreign key references. 实际上,您实际上应该为所有表都使用整数主键,并将其用于外键引用。 If you need data in the output in the question, then just write a query to return it in that format. 如果在问题的输出中需要数据,则只需编写查询以该格式返回它。

Looking at the table structure, even I think it will not be possible to write joins because as you mentioned, the table structure doesn't have a relation between them. 从表结构来看,即使我认为也无法编写联接,因为正如您所提到的,表结构之间没有关系。

I am assume you have just started the project, so it's time you first re-visit your DB structure and correct it. 我假设您刚刚启动了该项目,所以现在是时候重新访问您的数据库结构并对其进行更正了。

Ideally, you should have a customer table with a customer id. 理想情况下,您应该有一个带有客户ID的客户表。 Then you should have products table with product id. 然后,您应该具有带有产品ID的产品表。 One table which will have data on what products customers have purchased - something like customer_products. 一个表,其中包含有关客户购买了哪些产品的数据-例如customer_products。 This will be a one to many relation. 这将是一对多的关系。 So customer 1 can have product 1,3 and 5. Which would means in customer_product there will be three entries. 因此,客户1可以拥有产品1,3和5。这意味着在customer_product中将存在三个条目。

And then when you want to do a sum total, you can first join the table customer, product based on customer_product and then do a sum of the price also to get the total amount for individual customer. 然后,当您要进行总计时,您可以首先将表customer加入到基于customer_product的产品中,然后再对价格进行求和,以获得单个客户的总金额。

Bad design. 设计不良。 You must a have a customers table like this: 您必须有一个customers表,如下所示:

custumer_id
customer_name
other fileds...

On the other hand you should have an accesories table, where you usually describe each item- 另一方面,您应该有一个配件表,通常在其中描述每个项目-

accesory_id
accesory_name
supplier_id
country_of_origin
other stuff

Also an accesory_price table, where prices are added due the fact that prices change. 也是accesory_price表,其中由于价格变化而在其中添加价格。

accesory_id
price
active_price
date_price_added

An finally you should relate all in a customer_accesory table: 最后,您应该将所有内容关联到一个customer_accesory表中:

customer_id
accesory_id

By having this, you can join tables and select both customer basket size and customer preferences of accesories. 通过此操作,您可以联接表并选择客户购物篮大小和客户偏好的配件。 Basket size, or the amount purchased by each customer can be summarized SUM , AVG , COUNT or you can pivot data using GROUP_CONCAT in order to generate high quality reports. 购物篮大小或每个客户购买的金额都可以汇总为SUMAVGCOUNT ,也可以使用GROUP_CONCAT进行数据透视以生成高质量的报告。

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

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