MySQL,使用另一个表中的列作为CASE语句

MySQL,使用另一个表中的列作为CASE语句,mysql,Mysql,我有两个表,一个Items表,如下所示: 项目 Item Count ----------------- 1 20 2 24 3 49 还有一个名为ItemsConfig的表,如下所示: 项目配置 ItemCountLow ItemCountHigh ItemCountStatus ------------------------------------------------ 0 2

我有两个表,一个Items表,如下所示:


项目

Item       Count
-----------------
1          20
2          24
3          49
还有一个名为ItemsConfig的表,如下所示:


项目配置

ItemCountLow    ItemCountHigh    ItemCountStatus
------------------------------------------------
0               20               Low
21              50               Normal
51              100              Surplus
我想做的是构建一个查询,使用ItemsConfig表中的
ItemCountLow
ItemCountHigh
阈值比较Items表中的项目计数,以导出ItemCountStatus

下面是每个表的create语句

项目表

CREATE TABLE IF NOT EXISTS `Items` (
  `Item` int(11) NOT NULL,
  `Count` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `Items`
--

INSERT INTO `Items` (`Item`, `Count`) VALUES
(1, 20),
(2, 24),
(3, 49);
CREATE TABLE IF NOT EXISTS `ItemsConfig` (
  `ItemCountLow` int(11) NOT NULL,
  `ItemCountHigh` int(11) NOT NULL,
  `ItemCountStatus` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `ItemsConfig`
--

INSERT INTO `ItemsConfig` (`ItemCountLow`, `ItemCountHigh`, `ItemCountStatus`) VALUES
(0, 20, 'Low'),
(21, 50, 'Normal'),
(51, 100, 'Surplus');
项目配置表

CREATE TABLE IF NOT EXISTS `Items` (
  `Item` int(11) NOT NULL,
  `Count` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `Items`
--

INSERT INTO `Items` (`Item`, `Count`) VALUES
(1, 20),
(2, 24),
(3, 49);
CREATE TABLE IF NOT EXISTS `ItemsConfig` (
  `ItemCountLow` int(11) NOT NULL,
  `ItemCountHigh` int(11) NOT NULL,
  `ItemCountStatus` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `ItemsConfig`
--

INSERT INTO `ItemsConfig` (`ItemCountLow`, `ItemCountHigh`, `ItemCountStatus`) VALUES
(0, 20, 'Low'),
(21, 50, 'Normal'),
(51, 100, 'Surplus');
我正试着做这样的事情,但似乎想不出来

SELECT item, count, 
CASE
    WHEN count > ItemsConfig.ItemCountLow AND count < ItemsConfig.ItemCountHigh  
        THEN ItemsConfig.ItemCountStatus
END as 'status'
FROM Items
选择项目,计数,
案例
当count>ItemsConfig.ItemCountLow和count
有人能帮忙吗

那么:

SELECT i.item, i.count, ifnull(ic.ItemCountStatus,'Unknown') status
FROM items i
LEFT JOIN ItemsConfig ic on (i.count between ic.ItemCountLow  AND ic.ItemCountHigh)

如果
计数
超过
ItemsCount
中的任何限制,我建议使用
左连接
。在这里,我正在制作这个演示。。。。我在查询中添加了一个“未知”状态,该状态显示何时连接条件为false。