0

私は、単一のテーブルからのトリッキーな出力を必要とするプロジェクトレポートに取り組んでいます。

これは、daily_visit_expensesという名前のテーブルの構造です。 daily_visit_expenses

このようなデータを持つ:: ここに画像の説明を入力してください

ここで出力したいのは、これら3つのクエリを1つの列(任意の名前)に組み合わせたものです。クエリは次のとおりです。

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'

のような単一の列に上記の3つのクエリを組み合わせた単一のクエリから出力を取得したいと思い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

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';

編集:UNIONDISTINCTに変更

于 2012-06-26T07:43:32.007 に答える
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 に答える
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 に答える