SQL search multitable and contains - mysql

Suppose that i have the following data:
CREATE TABLE IF NOT EXISTS `Class` (
`id` INT NOT NULL AUTO_INCREMENT,
`class name` CHAR(55) NOT NULL,
PRIMARY KEY (`id`)
);
REPLACE INTO `Class` (`id`,`class name`) VALUES
(1,'Mammalia'),(2, 'Amphibia'),(3, 'Aves'),(4, 'Reptile');
CREATE TABLE IF NOT EXISTS `Animals` (
`id` INT NOT NULL AUTO_INCREMENT,
`class_id` INT DEFAULT NULL,
`name` CHAR(55) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`class_id`) REFERENCES `Class`(`id`)
);
INSERT INTO `Animals` (`class_id`,`name`) VALUES
(1,'Przewalskis horse'),
(1,'Bos taurus'),
(1,'Sus scrofa'),
(1,'Panthera leo'),
(1,'Felis catus'),
(1,'Canis lupus'),
(1,'Pan troglodytes'),
(1,'Nasalis larvatus'),
(1,'Ailuropoda melanoleuca'),
(1,'Elephas maximus sumatranus'),
(1,'Panthera pardus orientalis'),
(2, 'Bufu Bufu'),
(2, 'Notophthalmus viridescens'),
(2, 'Dermophis mexicanus'),
(3, 'Campephilus melanoleucos melanoleucos'),
(3, 'Tyto alba'),
(3, 'Cathartes aura'),
(3, 'Serinus canaria'),
(3, 'Amazona aestiva'),
(3, 'Amazona Oratrix'),
(3, 'Anodorhynchus hyacinthinus'),
(3, 'Ara ararauna'),
(3, 'Ara chloropterus'),
(3, 'Strigops habroptilus'),
(4, 'Ameivula venetacaudus'),
(4, 'Chelonia mydas'),
(4, 'Caretta caretta');
I want to do a query with only one input html field that return the following results:
if i type: "Amphibia" returns:
-------------------
| Class | Animals |
-------------------
| Amphibia | Bufu Bufu |
| Amphibia | Notophthalmus viridescens |
| Amphibia | Dermophis mexicanus |
----------------------------------
if i type: "Amphibia Amazona" returns:
-------------------
| Class | Animals |
-------------------
| Amphibia | Bufu Bufu |
| Amphibia | Notophthalmus viridescens |
| Amphibia | Dermophis mexicanus |
| Aves | Amazona aestiva |
| Aves | Amazona Oratrix |
--------------------------
if i type: "caudu" returns
-------------------
| Class | Animals |
-------------------
| Reptile | Ameivula venetacaudus |
--------------------------
Sample codes in Fiddle are welcome!
Thankful right now

Considering that input_var is the variable that you will receive from your HTML, you can do the following query:
SELECT `class name`, name
FROM Animals
LEFT JOIN Class
ON (Class.id= Animals.class_id)
WHERE `class name` like concat('%',SUBSTRING_INDEX(SUBSTRING_INDEX(input_var, ' ', 1), ' ', -1),'%')
OR name like concat('%',SUBSTRING_INDEX(SUBSTRING_INDEX(input_var, ' ', 1), ' ', -1),'%')
OR `class name` like concat('%',SUBSTRING_INDEX(SUBSTRING_INDEX(input_var, ' ', 2), ' ', -1),'%')
OR name like concat('%',SUBSTRING_INDEX(SUBSTRING_INDEX(input_var, ' ', 2), ' ', -1),'%')
;
I am also considering that you can receive an input with the inversed animal/class order, like "Amazona Amphibia" instead of "Amphibia Amazona".
If may also consider if case sensitive is important to you. If not, you may use upper in both your input string and the table column.

Keep this as your base.
select id, `class name`, `name`
from animals
left join class
on (class.id= animals.class_id)
And for searching
select `class name`, `name` from
(select ...)
where concat("%", "$search1", "%") like `class name` or
concat("%", "$search2", "%") like `name`;
To split the first word (mysql)
substring_index(substring_index(var, ' ', 1), ' ', -1)
and for second word (mysql)
substring_index(substring_index(var, ' ', 2), ' ', -1)
In PHP you can use substr function.

Related

ORDER by multiple queries in MySql

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 |
+----+------------+

How to add a character to specific index in string

I have a db with email-addresses which are 15 characters in total.
Some are 14 characters long and they have the same in common, they miss a 0 in their name at the 3th index.
I had a what similar problem with the id numbers but that was easy to fix because i had to push 0 to the first index.
My expected results should be changing 'aa-001#test.me' to 'aa-0001#test.me'.
To add a 0 as 4th character in emails having 14 characters, you may simply use SUBSTR :
SELECT CONCAT(SUBSTR(email, 1, 3), '0', SUBSTR(email, 4))
FROM mytable
WHERE LENGTH(email) = 14
Another solution is to use REGEXP_REPLACE, with a regular expression that matches only on emails with 14 characters ; this avoids the need for a WHERE clause, as emails that do not match the pattern will be returned untouched :
SELECT REGEXP_REPLACE(email, '^(.{3})(.{11})$', CONCAT('$1', '0', '$2'))
FROM t
db<>fiddle here :
WITH t AS (SELECT 'aa-001#test.me' email UNION SELECT 'bb-0002#test.me')
SELECT CONCAT(SUBSTR(email, 1, 3), '0', SUBSTR(email, 4))
FROM t
WHERE LENGTH(email) = 14
| CONCAT(SUBSTR(email, 1, 3), '0', SUBSTR(email, 4)) |
| :----------------------------------------------------- |
| aa-0001#test.me |
WITH t AS (SELECT 'aa-001#test.me' email UNION SELECT 'bb-0002#test.me')
SELECT REGEXP_REPLACE(email, '^(.{3})(.{11})$', CONCAT('$1', '0', '$2'))
FROM t
| REGEXP_REPLACE(email, '^(.{3})(.{11})$', CONCAT('$1', '0', '$2')) |
| :---------------------------------------------------------------- |
| aa-0001#test.me |
| bb-0002#test.me

Sum the digits of a number in mysql

I have a query that returns an integer number from a mathematical calculation. I need to sum all the digits in that integer number.
Something like this:
select sumdigits(number) from dual
-- if number =123, output: 1+2+3 = 6
-- if number =100, output: 1+0+0 = 1
I wanted to test this using Fiddle or Rextester, but neither is working right now. So, your upvotes/downvotes will serve as the test:
SELECT CAST(SUBSTRING(number, 1, 1) AS UNSIGNED) + -- first digit
CAST(SUBSTRING(number, 2, 1) AS UNSIGNED) + -- second digit
CAST(SUBSTRING(number, 3, 1) AS UNSIGNED) AS the_sum -- third digit
FROM yourTable
This assumes a number with a max width of 3 digits, which is also zero padded (as you mentioned we can assume).
If you really need to do this in production, you should probably create a user defined function to handle such manipulations, edge cases, etc.
Update:
Going with the comment by #ThorstenKettner we could generalize this answer to a number field of any length by just adding more terms for each digit position. For example, if we wanted to cover numbers which could be up to four digits wide we could just add this term:
+ CAST(SUBSTRING(number, 4, 1) AS UNSIGNED)
which would either add a number if present, or would add zero if not present.
I'm ashamed to even suggest this but...
SELECT
foo,
CHAR_LENGTH(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(
REPLACE(foo, '-', ''),
'0', ''),
'1', '1'),
'2', '22'),
'3', '333'),
'4', '4444'),
'5', '55555'),
'6', '666666'),
'7', '7777777'),
'8', '88888888'),
'9', '999999999')
) AS digit_sum
FROM (
SELECT 123 AS foo
UNION ALL SELECT 100
UNION ALL SELECT 413432143
UNION ALL SELECT -6301
UNION ALL SELECT 1234567890
) x
+------------+-----------+
| foo | digit_sum |
+------------+-----------+
| 123 | 6 |
| 100 | 1 |
| 413432143 | 25 |
| -6301 | 10 |
| 1234567890 | 45 |
+------------+-----------+
5 rows in set (0.00 sec)
It probably makes more sense rewritten as function, together with some error checking to return NULL on floats or something similar.

How to remove null values in query using CONCAT with CASE statement

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.

mysql 5.6: group_concat help sought

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