简体   繁体   English

MySQL 将来自两个单独且复杂的查询的列合并为一行

[英]MySQL combine columns from two separate and complex queries into one row

I'm working on a website where there are many distinct lines of garments.我正在一个网站上工作,那里有许多不同的服装系列。 A line is defined as garments of the same design, style, manufacturer etc. Within a line, garments come in different sizes and colours.一条线被定义为具有相同设计、风格、制造商等的服装。在一条线上,服装有不同的尺寸和颜色。

To compose a chart of all the colours available in one particular line the following query works fine (I'm working in Coldfusion but PHP would use the same query).要组成一个特定行中所有可用颜色的图表,以下查询可以正常工作(我在 Coldfusion 工作,但 PHP 将使用相同的查询)。

SELECT 
skuColourwayID,cHex,cLongName,skuID, cwColID1
from garment_sku
join garment_colourways
on cwID=skuColourwayID
join garment_colour_name
on cID=cwColID1
where skuLineID= <cfqueryPARAM value = "#url.lnID#" CFSQLType = "CF_SQL_INTEGER">
group by skuColourwayID

In order to accumulate all the info I need, I have to access three tables using joins.为了积累我需要的所有信息,我必须使用连接访问三个表。 (I don't have any choice about how the data is presented to me). (对于如何向我呈现数据,我没有任何选择)。 The line is identified by lnID which in the above case starts as a url variable.该行由 lnID 标识,在上述情况下,它以 url 变量开头。 Starting with the sku table (garment_sku) I access the colourways table (garment_colourways) and get the colourway colour id (cwColID1).从 sku 表 (garment_sku) 开始,我访问 colourways 表 (garment_colourways) 并获取 colourway 颜色 ID (cwColID1)。 By applying this to the colour name table (garment_colour_name) I can get the actual name of the colour, and it's hex value.通过将其应用于颜色名称表(garment_colour_name),我可以获得颜色的实际名称,它是十六进制值。

This all works perfectly, EXCEPT a few garments come in bi-colours (ie different colour sleeves to body or collar etc).这一切都很完美,除了一些服装是双色的(即不同颜色的袖子到身体或衣领等)。 There is a second column in the garment_sku table, cwColID2 which denotes the second colour.服装sku 表中有第二列,cwColID2,它表示第二种颜色。

One way around this would be to perform two separate queries, where cwColID1 is replaced by cwColID2 in the second query.解决此问题的一种方法是执行两个单独的查询,其中 cwColID1 在第二个查询中被 cwColID2 替换。 I could then combine the queries programatically to achive the bi-colour where required.然后我可以以编程方式组合查询以在需要的地方获得双色。 However, this seems rather inelegant and I'm sure MySQL has a way of dealing with this in one query?但是,这似乎相当不雅,我敢肯定 MySQL 有办法在一个查询中处理这个问题?

I hate to say it but there is also provison in the tables for a third colourway cwColID3 though I've never come across any three colour garments and would be very happy to solve this for just the two colours.我不想这么说,但表中还提供了第三种颜色 cwColID3 的规定,尽管我从未遇到过任何三种颜色的服装,并且很乐意只为这两种颜色解决这个问题。

Thanks for any help you can give.谢谢你提供的所有帮助。

You just need to JOIN to the garment_colour_name table a second time to get the name of the second colour.您只需要再次JOINgarment_colour_name颜色名称表中即可获得第二种颜色的名称。 Note that since not all garments will have 2 colours, you need to use a LEFT JOIN rather than an INNER JOIN :请注意,由于并非所有服装都有 2 种颜色,因此您需要使用LEFT JOIN而不是INNER JOIN

SELECT 
    skuColourwayID,
    gc1.cHex AS c1Hex, 
    gc1.cLongName AS c1Name,
    gc2.cHex AS c2Hex,
    gc2.cLongName AS c2Name,
    skuID, 
    cwColID1,
    cwColID2
from garment_sku
join garment_colourways
on cwID=skuColourwayID
join garment_colour_name gc1
on gc1.cID=cwColID1
left join garment_colour_name gc2
on gc2.cID=cwColID2
where skuLineID= <cfqueryPARAM value = "#url.lnID#" CFSQLType = "CF_SQL_INTEGER">
group by skuColourwayID

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

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