我正在制作项目报告,需要从单个表格中获得一些棘手的输出。
这是名为 daily_visit_expenses 的表的结构
有这样的数据::
现在我想要的输出是这三个查询在一个列(任何名称)中的组合,查询是:
SELECT DISTINCT(`cust_1`) FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_1`='Sales'
SELECT DISTINCT(`cust_2`) FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_2`='Sales'
SELECT DISTINCT(`cust_3`) FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_3`='Sales'
我想从单个查询中获取输出,该查询是单个列中的上述三个查询的组合,例如customers
DISTINCT
。
SQL导出:
CREATE TABLE IF NOT EXISTS `daily_visit_expenses` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user` varchar(255) NOT NULL,
`date` date NOT NULL,
`status` varchar(50) NOT NULL,
`location` varchar(50) NOT NULL,
`cust_1` varchar(255) NOT NULL,
`cust_2` varchar(255) NOT NULL,
`cust_3` text NOT NULL,
`cust_4` text NOT NULL,
`purpose_1` varchar(20) NOT NULL,
`purpose_2` varchar(20) NOT NULL,
`purpose_3` varchar(20) NOT NULL,
`purpose_4` varchar(20) NOT NULL,
`visit_type` varchar(20) NOT NULL,
`expenses` bigint(20) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
INSERT INTO `daily_visit_expenses` (`id`, `user`, `date`, `status`, `location`, `cust_1`, `cust_2`, `cust_3`, `cust_4`, `purpose_1`, `purpose_2`, `purpose_3`, `purpose_4`, `visit_type`, `expenses`) VALUES
(5, 'sanjay', '2012-06-15', 'Working', 'Customer', 'S.G.R.S. Chemical & Minral Industries', '', 'fatehpur foundry', '', 'Sales', '', 'Sales', '', 'Outstation', 2323),
(8, 'sanjay', '2012-06-25', 'Working', 'Office', '', '', '', '', '', '', '', '', '', 0),
(9, 'sanjay', '2012-06-09', 'Working', 'Office', '', '', '', '', '', '', '', '', '', 0),
(10, 'sanjay', '2012-06-05', 'Working', 'Customer', 'V.N INTERNATIONAL', 'G.SURGIWEAR', '', '', 'Sales', 'Sales', '', '', 'Outstation', 332),
(11, 'sanjay', '2012-06-30', 'Working', 'Customer', 'Ganesh Plywood-Sitapur', '', '', '', 'Service', '', '', '', 'Outstation', 434),
(12, 'sanjay', '2012-06-04', 'Absent', '', '', '', '', '', '', '', '', '', '', 0),
(13, 'sanjay', '2012-06-06', 'Absent', '', '', '', '', '', '', '', '', '', '', 0),
(14, 'sanjay', '2012-06-08', 'Leave', '', '', '', '', '', '', '', '', '', '', 0);
答案 0 :(得分:1)
尝试使用UNION
SELECT DISTINCT(`cust_1`) FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_1`='Sales'
UNION DISTINCT
SELECT DISTINCT(`cust_2`) FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_2`='Sales'
UNION DISTINCT
SELECT DISTINCT(`cust_3`) FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_3`='Sales';
编辑:已更改为UNION DISTINCT
答案 1 :(得分:1)
mysql> SELECT DISTINCT(`customer`) FROM ( SELECT `cust_1` as `customer` FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_1`='Sales' UNION ALL SELECT `cust_2` as `customer` FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_2`='Sales' UNION ALL SELECT `cust_3` as `customer` FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_3`='Sales') as t;
+---------------------------------------+
| customer |
+---------------------------------------+
| S.G.R.S. Chemical & Minral Industries |
| V.N INTERNATIONAL |
| G.SURGIWEAR |
| fatehpur foundry |
+---------------------------------------+
4 rows in set (0.00 sec)
答案 2 :(得分:0)
试试这个,我想如果我没有误解你的问题就是你所需要的:
SELECT DISTINCT(`customer`) FROM
(
SELECT `cust_1` FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_1`='Sales' UNION ALL
SELECT `cust_2` FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_2`='Sales' UNION ALL
SELECT `cust_3` FROM `daily_visit_expenses` WHERE user='sanjay' AND `purpose_3`='Sales')
答案 3 :(得分:-2)
这应该有效:
SELECT DISTINCT(`cust_1`,`cust_2`,`cust_3`) FROM `daily_visit_expenses`
WHERE user='sanjay' AND (`purpose_1`='Sales' OR `purpose_2`='Sales' OR
`purpose_3`='Sales' )