简体   繁体   English

创建一个条件SQL选择语句,该语句根据MYSQL中的计数插入

[英]Creating a conditional SQL select statement that inserts based on a count in MYSQL

I am trying to develop a statement that counts the sizes of a product and insert a statement base on the amount of that count. 我正在尝试开发一种对产品尺寸进行计数的语句,并根据该计数值插入一个语句。

For Example, 例如,

I have a shirt that has 3 sizes (S, M, L) For reach size I would need to insert a SQL statement. 我有一件衬衫,它有3种尺寸(S,M,L)。要达到范围尺寸,我需要插入一条SQL语句。 The insert statement is to insert on Table 2 the menu position. insert语句是在表2上插入菜单位置。

Table1 表格1

ID |  Product | Size
1      Shirt      S  
2      Shirt      M
3      Shirt      L

Table2 表2

ID  |  Dropdown_menu_Position
1        0
2        1
3        2

I know the following query is incorrect but I am struggling with the logic behind it. 我知道以下查询是错误的,但我正在努力解决其背后的逻辑。 Any SQL guru can help solve or guide me in the right direction? 任何SQL专家都可以帮助解决或指导我正确的方向吗?

INSERT INTO Table2
CASE
WHEN COUNT (SIZE) = 1 THEN
     SELECT NULL, '0' 
WHEN COUNT (SIZE) = 2 THEN
     SELECT NULL, '1' 
WHEN COUNT (SIZE) = 3 THEN
     SELECT NULL, '2' 
ELSE ''
END

I am new at making CASE statement and maybe an IF statement would work better for this, but neither one I know. 我是CASE语句的新手,也许IF语句可以更好地解决这个问题,但是我都不知道。

IF you are using MySQL 8.0, you can use window functions for this. 如果您使用的是MySQL 8.0,则可以使用窗口功能。 They can be used to generate a numbering based on a group: 它们可用于基于组生成编号:

insert into Table2(Id, Dropdown_menu_Position)
select
  t.ID,
  --t.Product,
  --t.Size,
  ROW_NUMBER() over (
    order by s.SortOrder
  ) partition by (t.Product) as Dropdown_menu_Position
from
  Table1 t
  inner join (
    -- Inlined now, but maybe you can make a lookup table for this
    select 'XS' as Size, 0 as SortOrder
    union all select 'S', 1
    union all select 'M', 2
    union all select 'L', 3
    union all select 'XL', 4
    union all select 'XXL', 5
  ) s on s.Size = t.Size

In earlier versions of MySQL, these functions don't exist, and this won't work. 在早期版本的MySQL中,这些功能不存在,因此无法使用。 However, for this particular case, you should indeed be able to use count to check how many rows there are before the current one. 但是,对于这种特殊情况,您确实应该能够使用count来检查当前行之前有多少行。 If you have a separate table for the sizes, this query is quite readable, but if not, you would have to embed the lookup table twice in the query. 如果您有一个单独的表来查询尺寸,则此查询非常容易阅读,但如果没有,则必须在查询中两次嵌入查找表。 The sample below assumed a separate table: 下面的示例假定了一个单独的表:

-- Assuming a lookup table like this:
create table Sizes as
  select 'XS' as Size, 0 as SortOrder
  union all select 'S', 1
  union all select 'M', 2
  union all select 'L', 3
  union all select 'XL', 4
  union all select 'XXL', 5;

-- Select all IDs and how often a smaller size exists for that product
insert into Table2(Id, Dropdown_menu_Position)
select
  t.ID,
  ( select count(*)
    from 
      Table1 t2
      inner join Sizes s2 on s2.Size = t2.Size 
    where
      t2.Product = t.Product and
      s2.SortOrder < s.SortOrder) as Dropdown_menu_Position
from
  Table1 t
  inner join Sizes s on s on s.Size = t.Size

But, maybe you don't need the numbers in Table2 to be consecutive. 但是,也许您不需要表2中的数字是连续的。 Maybe it's okay, as long as they have the right order (so M is below S in the menu). 只要它们的顺序正确(也许M在菜单中的S之下),也许就可以了。 This will, of course, depend on your code, but if that is possible, you can simply write this: 当然,这取决于您的代码,但是如果可能的话,您可以简单地编写以下代码:

insert into Table2(Id, Dropdown_menu_Position)
select
  t.ID,
  s.SortOrder
from
  Table1 t
  inner join (
    -- Inlined now, but maybe you can make a lookup table for this
    select 'XS' as Size, 0 as SortOrder
    union all select 'S', 1
    union all select 'M', 2
    union all select 'L', 3
    union all select 'XL', 4
    union all select 'XXL', 5
  ) s on s.Size = t.Size

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

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