0

我正在编写一个项目报告,该报告需要来自单个表的一些棘手的输出。

这是名为:daily_visit_expenses的表的结构 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);
4

4 回答 4

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)
于 2012-06-26T07:55:30.823 回答
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

于 2012-06-26T07:43:32.007 回答
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')
于 2012-06-26T07:48:30.290 回答
-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' )
于 2012-06-26T07:50:56.537 回答