I am trying to build a chat list page where latest sent/received contact is shown at the top from one table. For this, I have a table smshistory where i store sent/received sms with numbers where one is company phone and other is client phone number
CREATE TABLE IF NOT EXISTS `smshistory` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fromnumber` varchar(20) NOT NULL,
`tonumber` varchar(20) NOT NULL,
`sms` varchar(20) NOT NULL,
`added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
--
-- Dumping data for table `smshistory`
--
INSERT INTO `smshistory` (`id`, `fromnumber`, `tonumber`, `sms`, `added`) VALUES
(1, 'companynum', 'client1num', 'Hello', '2021-07-16 12:28:23'),
(2, 'companynum', 'client2num', 'Hello', '2021-07-16 12:28:23'),
(3, 'companynum', 'client3num', 'Hello', '2021-07-16 12:28:23'),
(4, 'client1num', 'companynum', 'Hi', '2021-07-16 12:28:23'),
(5, 'companynum', 'client1num', 'Hello', '2021-07-16 12:28:23'),
(6, 'client1num', 'companynum', 'Hi', '2021-07-16 12:28:23'),
(7, 'client2num', 'companynum', 'Hi', '2021-07-16 12:28:23'),
(8, 'companynum', 'client2num', 'Hello', '2021-07-16 12:28:23'),
(9, 'client3num', 'companynum', 'Hi', '2021-07-16 12:28:23');
As first message will always be from company number, so I am showing DISTINCT list with:
SELECT DISTINCT (`tonumber`) FROM `smshistory` WHERE `fromnumber` = $companynum
Which gives me list like:
client1num
client2num
client3num
Requirement:
What I require is to show the DISTINCT with order of added DESC column in a way that if a client's number is in fromnumber or tonumber, it should show at top. So, according to my table, results should be:
client3num
client2num
client1num
Fiddle is at http://sqlfiddle.com/#!9/4256d1d/1
Any idea on how to achieve that?
In answer to your question you can use the following query:
SELECT distinct client_num from (
SELECT CASE WHEN fromnumber = 'companynum' THEN tonumber
ELSE fromnumber END client_num
FROM smshistory ORDER BY id DESC ) as a
For each of the rows that contain $companynum either in fromnumber or in tonumber you must extract the client's number with a CASE expression and use GROUP BY to remove duplicates.
Finally, sort the results by the max value of added:
SELECT CASE WHEN fromnumber = $companynum THEN tonumber ELSE fromnumber END client_num
FROM smshistory
WHERE $companynum IN (fromnumber, tonumber)
GROUP BY client_num
ORDER BY MAX(added) DESC
In answer to the question 'How do I sort a de-normalised collection of strings (that a follow a very constrained pattern) according to the numerals contained within those strings?', here's one method...
DROP TABLE IF EXISTS x;
CREATE TABLE x
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,bad_string VARCHAR(20) NOT NULL
);
INSERT INTO x VALUES (11,'client2num'),(17,'client3num'),(21,'client1num');
SELECT REPLACE(REPLACE(bad_string,'client',''),'num','') p FROM x;
+---+
| p |
+---+
| 2 |
| 3 |
| 1 |
+---+
...which can be rewritten as follows:
SELECT * FROM x ORDER BY REPLACE(REPLACE(bad_string,'client',''),'num','') DESC;
+----+------------+
| id | bad_string |
+----+------------+
| 17 | client3num |
| 11 | client2num |
| 21 | client1num |
+----+------------+
I want to ignore null value from query .
I tried with case statement inside concat but its give me null values
The table structure
CREATE TABLE `UserInfo` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`key` varchar(200) NOT NULL,
`value` longtext NOT NULL,
`created_on` varchar(200) NOT NULL,
`updated_on` varchar(200) NOT NULL,
`status` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=111 DEFAULT CHARSET=latin1;
insert into `UserInfo`(`id`,`user_id`,`key`,`value`,`created_on`,`updated_on`,`status`)
values (69,11132,'country','India','','',1),(67,11132,'city','Gurgaon','','',1),(68,11132,'state','Haryana','','',1),(66,11132,'address','CyberHub','','',1),(64,11131,'state','Delhi','','',1),(65,11131,'country','India','','',1),(63,11131,'city','','','',1),(62,11131,'address','Lajpat Nagar','','',1),(60,11130,'state','Haryana','','',1),(61,11130,'country','India','','',1),(58,11130,'address','','','',1),(59,11130,'city','Gurgaon','','',1),(787,11130,'password','0192023a7bbd73250516f069df18b500','30-03-2016 06:58:18','30-03-2016 06:58:18',1),(788,11131,'password','04d237e63ebc1acbb3fcacf3e1a846cb','30-03-2016 06:58:18','26-04-2016 07:46:33',1),(789,11132,'password','0192023a7bbd73250516f069df18b500','30-03-2016 06:58:18','24-05-2016 11:47:35',1),(1123,11130,'parent','0','','',1),(1124,11131,'parent','0','','',1),(1125,11132,'parent','0','','',1),(2036,11136,'gcm_id','fLJ8vwS5yWc','04-05-2016 06:01:07','04-05-2016 06:01:07',1),(2035,11136,'push_id','fLJ8vwS5yWc:APA91bFO-hLW5uQqDQTABkilyOd9MzuMRhQMI8uNLRGh4fjaq3Bk1OvFmb7QFVKYpqqJZFPQ78Y1h0349IZuxq0EcxZ8VCHJOTOhhsqi1VxQ2A7TVLY-phDcN6sj80x8R7KoOocQKPLl','04-05-2016 06:01:07','04-05-2016 06:01:07',1),(2993,11570,'transaction_limit','100','16-09-2016 10:34:13','16-09-2016 10:34:13',1),(2992,11570,'otp','461178','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2991,11570,'password','1e28284f59e926547bb6793ad8723722','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2990,11570,'imei','353918057482479','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2989,11570,'push_id','dGxCE33MJgg:APA91bFCzSkvIDEcZcgmmSxVocFNZfLI8owLsdElmF-Et0wUH0mxUbQ9mIZDMjlhqClTOYiSxhSaVBdESwJW6J58hsyEF0LUMgXRTGdnEwWdbHJmm3EZuHDHzniMaJGcCKvALrcxtQt8','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2987,11570,'gender','Male','16-09-2016 10:32:48','16-09-2016 10:32:48',1),(2026,11136,'gcm_id','fLJ8vwS5yWc','03-05-2016 06:28:52','03-05-2016 06:28:52',1),(2025,11136,'push_id','fLJ8vwS5yWc:APA91bFO-hLW5uQqDQTABkilyOd9MzuMRhQMI8uNLRGh4fjaq3Bk1OvFmb7QFVKYpqqJZFPQ78Y1h0349IZuxq0EcxZ8VCHJOTOhhsqi1VxQ2A7TVLY-phDcN6sj80x8R7KoOocQKPLl','03-05-2016 06:28:52','03-05-2016 06:28:52',1),(2024,11136,'gcm_id','cU6PMImJ9Ms','02-05-2016 17:02:39','02-05-2016 17:02:39',1),(2023,11136,'push_id','cU6PMImJ9Ms:APA91bHA-a_joHtzEBgoAPRxRCXObeJLlCCNSxlCM-jBZKvSUEFvhLZEyqPbbsIDyWc2emZa1yBerOQLGXgzzNKZmcJYCgcF8KBdm9McVBiDKzU_OthjGnROZyTC5EvnI7Z4QivXnokA','02-05-2016 17:02:39','02-05-2016 17:02:39',1),(2022,11136,'gcm_id','cU6PMImJ9Ms','02-05-2016 16:38:55','02-05-2016 16:38:55',1),(2020,11136,'gcm_id','c2sOUeSpIuQ','02-05-2016 16:37:53','02-05-2016 16:37:53',1),(2021,11136,'push_id','cU6PMImJ9Ms:APA91bHA-a_joHtzEBgoAPRxRCXObeJLlCCNSxlCM-jBZKvSUEFvhLZEyqPbbsIDyWc2emZa1yBerOQLGXgzzNKZmcJYCgcF8KBdm9McVBiDKzU_OthjGnROZyTC5EvnI7Z4QivXnokA','02-05-2016 16:38:55','02-05-2016 16:38:55',1),(2019,11136,'push_id','c2sOUeSpIuQ:APA91bGskmvA5VVmxozMHKX3qHc16bdmk9h5gFTEPP8uFUYsO-doGCnkTE-ZtpMGeuuk2YCt3Ja56ey7nIga6aO7wpof2fI5zcgYdFACKvkcddNAlY4UhIO39tfyG3m4DZkmaLqw2Kxu','02-05-2016 16:37:53','02-05-2016 16:37:53',1),(2018,11136,'gcm_id','djRRW-PrS2Q','02-05-2016 16:30:02','02-05-2016 16:30:02',1),(2016,11136,'gcm_id','c2sOUeSpIuQ','02-05-2016 16:25:00','02-05-2016 16:25:00',1),(2017,11136,'push_id','djRRW-PrS2Q:APA91bFbB5SO0wY2TlGCZBmgbZUr0c1FXH5FE22YxOCf4tA7uM1V45T2cTY1aitIQKrf1bjrPB-zZUCDPNdL8-2SYnEawrFTdVjL5w1VuVI2kA89ixlK6jlpmNOoJ7wQnWqQoCFkcd9_','02-05-2016 16:30:02','02-05-2016 16:30:02',1),(2015,11136,'push_id','c2sOUeSpIuQ:APA91bGskmvA5VVmxozMHKX3qHc16bdmk9h5gFTEPP8uFUYsO-doGCnkTE-ZtpMGeuuk2YCt3Ja56ey7nIga6aO7wpof2fI5zcgYdFACKvkcddNAlY4UhIO39tfyG3m4DZkmaLqw2Kxu','02-05-2016 16:25:00','02-05-2016 16:25:00',1),(2014,11136,'gcm_id','djRRW-PrS2Q','01-05-2016 07:38:26','01-05-2016 07:38:26',1),(2013,11136,'push_id','djRRW-PrS2Q:APA91bFbB5SO0wY2TlGCZBmgbZUr0c1FXH5FE22YxOCf4tA7uM1V45T2cTY1aitIQKrf1bjrPB-zZUCDPNdL8-2SYnEawrFTdVjL5w1VuVI2kA89ixlK6jlpmNOoJ7wQnWqQoCFkcd9_','01-05-2016 07:38:26','01-05-2016 07:38:26',1),(2012,11136,'gcm_id','c2sOUeSpIuQ','01-05-2016 07:29:00','01-05-2016 07:29:00',1),(2011,11136,'push_id','c2sOUeSpIuQ:APA91bGskmvA5VVmxozMHKX3qHc16bdmk9h5gFTEPP8uFUYsO-doGCnkTE-ZtpMGeuuk2YCt3Ja56ey7nIga6aO7wpof2fI5zcgYdFACKvkcddNAlY4UhIO39tfyG3m4DZkmaLqw2Kxu','01-05-2016 07:29:00','01-05-2016 07:29:00',1),(2010,11439,'otp','467957','01-05-2016 07:04:42','16-08-2016 10:48:09',0),(2009,11438,'otp','631866','30-04-2016 09:36:54','06-05-2016 13:32:15',0),(2008,11438,'otp','586481','30-04-2016 09:22:55','06-05-2016 13:32:15',0),(2007,11438,'otp','971411','30-04-2016 09:19:52','06-05-2016 13:32:15',0),(2006,11136,'gcm_id','eNJISa-1OGA','30-04-2016 09:05:22','30-04-2016 09:05:22',1),(2005,11136,'push_id','eNJISa-1OGA:APA91bGB6m0d-RzoFUvw1SRsCIOEvfO3BjLUiMsZRdHnHlEGq6QKV9sVqCdLai25_dxTvUeuQVzFU5scKCaPvlDaG_VZ-V51t1AG4EEAtJdjGNQwhITF2eh0E24D_J9vPCQ3nIzanq8f','30-04-2016 09:05:22','30-04-2016 09:05:22',1),(2004,11445,'transaction_limit','2000','30-04-2016 09:03:41','30-04-2016 09:03:41',1),(2003,11445,'otp','230253','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(2002,11445,'password','25d55ad283aa400af464c76d713c07ad','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(2001,11445,'push_id','ccPZg_PD7bo:APA91bFrUK8JPZfI6tSBKDgETeeI_aE6RA-FTFh6pS4fluVv6jHIVKUyDAEurIJWo49nWa52q6Zas7F2DI_KNkDX-HVq_TXuO36dRN9JditQVGR9HUq8avS5QEzYA2gwYjyo7Bosrsyz','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(2000,11445,'gcm_id','ccPZg_PD7bo','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(1999,11445,'gender','Male','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(1998,11445,'bank_name','DCB Bank','30-04-2016 09:02:35','30-04-2016 09:02:35',1),(1997,11136,'gcm_id','cXK3F2L7Fcw','30-04-2016 08:37:01','30-04-2016 08:37:01',1)
I tried with query but its produce null column value as well
I want to ignore null value
SELECT
CONCAT(
CASE WHEN ui.key='gender' THEN ui.value ELSE '' END,
CASE WHEN ui.key='password' THEN ui.value ELSE '' END,
CASE WHEN ui.key='transaction_limit' THEN ui.value ELSE '' END
) AS test,
ui.id,ui.user_id,ui.key,ui.value
FROM UserInfo ui
WHERE ui.user_id=11445
Result :
------------------------------------------------------------------------------------------------------------------
test id user_id key value
2000 2004 11445 transaction_limit 2000
2003 11445 otp 230253
25d55ad 2002 11445 password 25d55ad..
2001 11445 push_id ccPZg_P..
2000 11445 gcm_id ccPZg_PD7bo
Male 1999 11445 gender Male
1998 11445 bank_name DCB Bank
------------------------------------------------------------------------------------------------------------------
Desire Result :
------------------------------------------------------------------------------------------------------------------
test id user_id key value
2000 2004 11445 transaction_limit 2000
25d55ad 2002 11445 password 25d55ad..
Male 1999 11445 gender Male
------------------------------------------------------------------------------------------------------------------
Sorry for the wron answer:
you can add Having:
SELECT
CONCAT(
CASE WHEN ui.key='gender' THEN ui.value ELSE '' END,
CASE WHEN ui.key='password' THEN ui.value ELSE '' END,
CASE WHEN ui.key='transaction_limit' THEN ui.value ELSE '' END
) AS test,
ui.id,ui.user_id,ui.key,ui.value
FROM UserInfo ui
WHERE ui.user_id=11445
HAVING test <> '';
sample
mysql> SELECT
-> CONCAT(
-> CASE WHEN ui.key='gender' THEN ui.value ELSE '' END,
-> CASE WHEN ui.key='password' THEN ui.value ELSE '' END,
-> CASE WHEN ui.key='transaction_limit' THEN ui.value ELSE '' END
-> ) AS test,
-> ui.id,ui.user_id,ui.key,ui.value
-> FROM UserInfo ui
-> WHERE ui.user_id=11445
-> HAVING test <> '';
+----------------------------------+------+---------+-------------------+----------------------------------+
| test | id | user_id | key | value |
+----------------------------------+------+---------+-------------------+----------------------------------+
| 2000 | 2004 | 11445 | transaction_limit | 2000 |
| 25d55ad283aa400af464c76d713c07ad | 2002 | 11445 | password | 25d55ad283aa400af464c76d713c07ad |
| Male | 1999 | 11445 | gender | Male |
+----------------------------------+------+---------+-------------------+----------------------------------+
3 rows in set (0,00 sec)
mysql>
You can use CONCAT_WS instead of CONCAT . The first arg by CONCAT_WS ist the separetor
sample
mysql> select CONCAT('a','b',NULL,'d','e');
+------------------------------+
| CONCAT('a','b',NULL,'d','e') |
+------------------------------+
| NULL |
+------------------------------+
1 row in set (0,01 sec)
mysql> select CONCAT_WS(',','a','b',NULL,'d','e');
+-------------------------------------+
| CONCAT_WS(',','a','b',NULL,'d','e') |
+-------------------------------------+
| a,b,d,e |
+-------------------------------------+
1 row in set (0,00 sec)
mysql>
Manual:
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated. The separator can be a string, as can the rest of the
arguments.
If the separator is NULL, the result is NULL; all other NULL values
are skipped. This makes CONCAT_WS() suitable when you want to
concatenate some values and avoid losing all information if one of
them is NULL.
I have the tables:
CREATE TABLE IF NOT EXISTS `buildingAccess` (
`id` int(10) unsigned NOT NULL,
`building` varchar(16) NOT NULL,
`person` varchar(16) NOT NULL,
`enteryDate` datetime NOT NULL,
`exitDate` datetime DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
INSERT INTO `buildingAccess` (`id`, `building`, `person`, `enteryDate`, `exitDate`) VALUES
(1, 'Lot B-3', 'Alice Jones', '2015-11-10 05:29:14', '2015-11-10 15:18:42'),
(3, 'Lot B-3', 'Alice Jones', '2015-11-11 07:11:27', '2015-11-11 12:43:34'),
(7, 'Lot B-3', 'Alice Jones', '2015-12-10 07:11:27', '2015-12-11 12:43:34'),
(2, 'Lot B-3', 'Bill Mayhew', '2015-11-10 10:29:14', '2015-11-10 12:18:42'),
(4, 'Lot B-3', 'Bill Mayhew', '2015-11-12 09:10:27', '2015-11-13 02:43:34'),
(8, 'Lot B-3', 'Bill Mayhew', '2015-11-12 09:10:27', '2015-11-13 02:43:34'),
(5, 'Lot B-3', 'Charlotte Ahn', '2015-12-01 05:29:14', NULL),
(6, 'Lot B-3', 'Dennis Lowe', '2015-12-10 10:29:14', '2015-12-10 12:18:42');
CREATE TABLE IF NOT EXISTS `buildingNotes` (
`building` varchar(16) NOT NULL,
`observer` varchar(16) NOT NULL,
`observationDate` datetime NOT NULL,
`note` varchar(64) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `buildingNotes` (`building`, `observer`, `observationDate`, `note`) VALUES
('Lot B-3', 'Alice Jones', '2015-11-10 05:32:12', 'burned out light on pole South-22'),
('Lot B-3', 'Alice Jones', '2015-11-10 05:39:12', 'burned out light on pole West-7'),
('Lot B-3', 'Alice Jones', '2015-11-10 05:42:12', 'overfull trash can near pole North-11'),
('Lot B-3', 'Charlotte Ahn', '2015-12-01 06:09:14', 'change drawr running low at gate 3');
ALTER TABLE `buildingAccess`
ADD PRIMARY KEY (`id`), ADD KEY `building` (`building`,`person`,`enteryDate`,`exitDate`);
ALTER TABLE `buildingNotes`
ADD KEY `building` (`building`,`observer`,`observationDate`,`note`);
ALTER TABLE `buildingAccess`
MODIFY `id` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=9;
My goal is a query that returns a list of all records in the buildingAccess table. Each should have a notes field that is the GROUP_CONCAT of all of the buildingNotes.note entries made during that record's dates/times bracketed by buildingAccess.enteryDate and buildingAccess.exitDate.
I have tried a few things but am stuck at:
select
BA.building,
BA.person,
BA.enteryDate,
IFNULL(BA.exitDate, NOW()),
IFNULL(
GROUP_CONCAT(
'<p>',
BN.observationDate, ': ',
BN.observer, ': ', BN.note,
'</p>'
ORDER BY BN.observationDate ASC
SEPARATOR ''
),
''
)
from
buildingAccess BA
LEFT JOIN buildingNotes BN ON
BN.building = BA.building
AND BN.observationDate BETWEEN BA.enteryDate AND BA.exitDate
group by BN.building
This returns:
+----------+---------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| building | person | enteryDate | exitDate | observations |
+----------+---------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Lot B-3 | Charlotte Ahn | 2015-12-01 05:29:14 | 2016-01-23 23:04:04 | |
| Lot B-3 | Alice Jones | 2015-11-10 05:29:14 | 2015-11-10 15:18:42 | <p>2015-11-10 05:32:12: Alice Jones: burned out light on pole South-22</p><p>2015-11-10 05:39:12: Alice Jones: burned out light on pole West-7</p><p>2015-11-10 05:42:12: Alice Jones: overfull trash can near pole North-11</p> |
+----------+---------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
I expected to see all of the other buildingAccess records even if there were no buildingNotes records.
I am assuming that I am not "grouping by" the right things but i have not found the right combination yet.
Pointers?
I believe it is due to use BN.building which is from the outer joined table in the GROUP BY clause. Try the following:
SELECT
BA.building
, BA.person
, BA.enteryDate
, IFNULL(BA.exitDate, NOW())
, IFNULL (
GROUP_CONCAT (
'<p>',
BN.observationDate, ': ',
BN.observer, ': ', BN.note,
'</p>'
ORDER BY BN.observationDate ASC
SEPARATOR ''
)
,''
)
FROM buildingAccess BA
LEFT JOIN buildingNotes BN ON BN.building = BA.building
AND BN.observationDate BETWEEN BA.enteryDate AND BA.exitDate
GROUP BY
BA.building
, BA.person
, BA.enteryDate
, IFNULL(BA.exitDate, NOW())
It s possible this is too many rows, and perhaps you need some other way to handle (for example) only getting the date rather than full datetime. However you should routinely specify ALL the non-aggregating columns of a query in the group by caluse. See MySQL GROUP BY Extension