仅显示唯一且重复的列
sqlfiddle :http://sqlfiddle.com/#!9/97bac/1
array(2) {
[0]=>
array(3) {
["itemId"]=>
string(4) "1001"
["itemName"]=>
string(3) "Dan"
["itemDesc"]=>
string(3) "Foo"
}
[1]=>
array(3) {
["itemId"]=>
string(4) "2002"
["itemName"]=>
string(3) "Bob"
["itemDesc"]=>
string(3) "Bar"
}
}
结果将是:
SELECT *
FROM `tbl_views_clicks`
WHERE `iDealID` =389
但我想要如下:
| iID | iUserID | iDealID | iCategoryID | iSubCategoryID | vType | eGainType | iLocationID | dtAdded | eDeviceType |
|------|---------|---------|-------------|----------------|----------|-----------|-------------|----------------------|-------------|
| 3410 | 1 | 389 | 79 | 135 | location | Instant | 1403 | 2017-08-21T11:26:51Z | Android |
| 3411 | 1 | 389 | 79 | 135 | location | ByClick | 0 | 2017-08-21T11:26:52Z | Android |
| 3412 | 1 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T11:27:19Z | Android |
| 3413 | 1 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T11:27:28Z | Android |
| 3414 | 1 | 389 | 79 | 135 | back | ByClick | 0 | 2017-08-21T11:27:32Z | Android |
| 3475 | 250 | 389 | 79 | 135 | location | Instant | 1403 | 2017-08-21T12:55:34Z | Android |
| 3476 | 250 | 389 | 79 | 135 | location | ByClick | 0 | 2017-08-21T12:55:36Z | Android |
| 3477 | 250 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T12:56:39Z | Android |
| 3479 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:04:30Z | Android |
| 3480 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:04:54Z | Android |
| 3481 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:05:06Z | Android |
| 3482 | 250 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T13:05:16Z | Android |
| 3483 | 250 | 389 | 79 | 135 | back | ByClick | 0 | 2017-08-21T13:05:21Z | Android |
| 3484 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:28Z | Android |
| 3485 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:36Z | Android |
| 3486 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:44Z | Android |
独特&重复结果:
-> if vType is gotit or back first time then CustomType = 'Unique' else CustomType = 'Repeat'
-> if vType is website first time then CustomType = 'Unique' else CustomType = 'Repeat'
最终结果:
| iID | iUserID | iDealID | iCategoryID | iSubCategoryID | vType | eGainType | iLocationID | dtAdded | eDeviceType | CustomType
|------|---------|---------|-------------|----------------|----------|-----------|-------------|----------------------|-------------|-------------
| 3410 | 1 | 389 | 79 | 135 | location | Instant | 1403 | 2017-08-21T11:26:51Z | Android | Unique
| 3411 | 1 | 389 | 79 | 135 | location | ByClick | 0 | 2017-08-21T11:26:52Z | Android | Delete
| 3412 | 1 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T11:27:19Z | Android | Unique
| 3413 | 1 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T11:27:28Z | Android | Repeat
| 3414 | 1 | 389 | 79 | 135 | back | ByClick | 0 | 2017-08-21T11:27:32Z | Android | Repeat
| 3475 | 250 | 389 | 79 | 135 | location | Instant | 1403 | 2017-08-21T12:55:34Z | Android | Unique
| 3476 | 250 | 389 | 79 | 135 | location | ByClick | 0 | 2017-08-21T12:55:36Z | Android | Delete
| 3477 | 250 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T12:56:39Z | Android | Unique
| 3479 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:04:30Z | Android | Unique
| 3480 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:04:54Z | Android | Repeat
| 3481 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:05:06Z | Android | Repeat
| 3482 | 250 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T13:05:16Z | Android | Repeat
| 3483 | 250 | 389 | 79 | 135 | back | ByClick | 0 | 2017-08-21T13:05:21Z | Android | Repeat
| 3484 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:28Z | Android | Unique
| 3485 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:36Z | Android | Unique
| 3486 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:44Z | Android | Unique
所以它将是:
| iID | iUserID | iDealID | iCategoryID | iSubCategoryID | vType | eGainType | iLocationID | dtAdded | eDeviceType | CustomType
|------|---------|---------|-------------|----------------|----------|-----------|-------------|----------------------|-------------|-------------
| 3410 | 1 | 389 | 79 | 135 | location | Instant | 1403 | 2017-08-21T11:26:51Z | Android | Unique
| 3411 | 1 | 389 | 79 | 135 | location | ByClick | 0 | 2017-08-21T11:26:52Z | Android | Delete // delete this
| 3412 | 1 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T11:27:19Z | Android | Unique
| 3413 | 1 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T11:27:28Z | Android | Repeat
| 3414 | 1 | 389 | 79 | 135 | back | ByClick | 0 | 2017-08-21T11:27:32Z | Android | Repeat // delete this
| 3475 | 250 | 389 | 79 | 135 | location | Instant | 1403 | 2017-08-21T12:55:34Z | Android | Unique
| 3476 | 250 | 389 | 79 | 135 | location | ByClick | 0 | 2017-08-21T12:55:36Z | Android | Delete // delete this
| 3477 | 250 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T12:56:39Z | Android | Unique
| 3479 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:04:30Z | Android | Unique
| 3480 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:04:54Z | Android | Repeat
| 3481 | 250 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:05:06Z | Android | Repeat // delete this
| 3482 | 250 | 389 | 79 | 135 | gotit | ByClick | 0 | 2017-08-21T13:05:16Z | Android | Repeat
| 3483 | 250 | 389 | 79 | 135 | back | ByClick | 0 | 2017-08-21T13:05:21Z | Android | Repeat // delete this
| 3484 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:28Z | Android | Unique
| 3485 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:36Z | Android | Repeat
| 3486 | 1 | 389 | 79 | 135 | website | ByClick | 0 | 2017-08-21T13:06:44Z | Android | Repeat // delete this
简而言之,所有第二个条目必须在CustomType列中定义为Repeat
怎么做?
答案 0 :(得分:1)
SELECT DISTINCT iUserID, iDealID, iCategoryID, iSubCategoryID, vType, eGainType, iLocationID, dtAdded, eDeviceType,CustomType
FROM
(SELECT *, CASE
WHEN INSTR(@VAR_COLUMN, vType) > 0 THEN 'Repeat'
ELSE 'Unique'
END CustomType, @VAR_COLUMN :=CONCAT(@VAR_COLUMN,CASE WHEN vType ='gotit' OR 'back' THEN 'gotit,back' ELSE vType END)
FROM `tbl_views_clicks`,
(SELECT @VAR_COLUMN :='')Z
GROUP BY CASE
WHEN vType IN ('gotit','back') THEN 1
ELSE vType
END,
CustomType
ORDER BY dtAdded)Z