Here is my code:
SELECT COALESCE(CONCAT(u.user_fname, ' ', u.user_lname), 'unknown') name
FROM users u
WHERE id = 10;
The result will be unknown when either user_fname or user_lname is null. That's not what I want, I want to select unknown only when both user_fname and user_lname are null.
Otherwise, I want to get the value of not-null column. How can I do that?
Use concat_ws():
SELECT CONCAT_WS(' ', u.user_fname, u.user_lname) name
FROM users u
WHERE id = 10;
This has the nice benefit that if either of the names are null, you don't get a spurious space in the result.
It gets a little tricky if you want to convert NULLs to "unknown". This should do the trick:
SELECT COALESCE(NULLIF(CONCAT_WS(' ', u.user_fname, u.user_lname) , ''), 'unknown') as name
FROM users u
WHERE id = 10;
You could use:
SELECT COALESCE(
TRIM(CONCAT(COALESCE(u.user_fname,''), ' ', COALESCE(u.user_lname,''))),
'unknown') name
FROM users u
WHERE id = 10;
I always like the isnull function in sql server (or nvl in oracle):
SELECT isnull(u.user_fname + ' ','') + isnull(u.user_lname, '') name
FROM users u
WHERE id=10
but then to switch include the Nulls, I would use a case:
SELECT
CASE WHEN u.user_fname IS NULL AND u.user_lname is NULL THEN 'unknown'
ELSE isnull(u.user_fname + ' ','') + isnull(u.user_lname, '') END name
FROM users u
WHERE id=10
yes, it's a little longer than the other answers, but easier to read and perhaps more flexible in the future in case you have other conditions.
Not really a performance hit either way, so it's down to personal preference.
Related
I have a multi-join query that targeting the hospital's chart database.
this takes 5~10 seconds or more.
This is the visual expain using mysql workbench.
The query is below.
select sc.CLIENT_ID as 'guardianId', sp.PET_ID as 'patientId', sp.NAME as 'petName'
, (select BW from syn_vital where HOSPITAL_ID = sp.HOSPITAL_ID and PET_ID = sp.PET_ID order by DATE, TIME desc limit 1) as 'weight'
, sp.BIRTH as 'birth', sp.RFID as 'regNo', sp.BREED as 'vName'
, (case when ss.NAME like '%fel%' or ss.NAME like '%cat%' or ss.NAME like '%pawpaw%' or ss.NAME like '%f' then '002'
when ss.NAME like '%canine%' or ss.NAME like '%dog%' or ss.NAME like '%can%' then '001' else '007' end) as 'sCode'
, (case when LOWER(replace(sp.SEX, ' ', '')) like 'male%' then 'M'
when LOWER(replace(sp.SEX, ' ', '')) like 'female%' or LOWER(replace(sp.SEX, ' ', '')) like 'fam%' or LOWER(replace(sp.SEX, ' ', '')) like 'woman%' then 'F'
when LOWER(replace(sp.SEX, ' ', '')) like 'c.m%' or LOWER(replace(sp.SEX, ' ', '')) like 'castratedmale' or LOWER(replace(sp.SEX, ' ', '')) like 'neutered%' or LOWER(replace(sp.SEX, ' ', '')) like 'neutrality%man%' or LOWER(replace(sp.SEX, ' ', '')) like 'M.N%' then 'MN'
when LOWER(replace(sp.SEX, ' ', '')) like 'woman%' or LOWER(replace(sp.SEX, ' ', '')) like 'f.s%' or LOWER(replace(sp.SEX, ' ', '')) like 'S%' or LOWER(replace(sp.SEX, ' ', '')) like 'neutrality%%' then 'FS' else 'NONE' end) as 'sex'
from syn_client sc
left join syn_tel st on sc.HOSPITAL_ID = st.HOSPITAL_ID and sc.CLIENT_ID = st.CLIENT_ID
inner join syn_pet sp on sc.HOSPITAL_ID = sp.HOSPITAL_ID and sc.FAMILY_ID = sp.FAMILY_ID and sp.STATE = 0
inner join syn_species ss on sp.HOSPITAL_ID = ss.HOSPITAL_ID and sp.SPECIES_ID = ss.SPECIES_ID
WHERE
trim(replace(st.NUMBER, '-','')) = '01099999999'
and trim(sc.NAME) = 'johndoe'
and sp.HOSPITAL_ID = 'HOSPITALID999999'
order by TEL_DEFAULT desc
I would like to know how to improve the performance of this complex query.
The most obvious performance killers in your query are the non-sargable criteria in your where clause.
trim(replace(st.NUMBER, '-','')) = '01099999999'
This cannot use any available index as you have applied a function to the column, which needs to be evaluated before the comparison can be made.
As suggested by Pham, you could change your criterion to -
st.number IN ('01099999999', '01-099-999-999', 'ALL_OTHERS_FORMAT_YOU_ACCEPTS...')
or better still would be to normalize the numbers before you store them (you can always apply formatting for display purposes), that way you know how to search the stored data. Strip all the hyphens and spaces from the existings numbers -
UPDATE syn_tel
SET number = REPLACE(REPLACE(number, '-',''), ' ', '')
WHERE number LIKE '% %' OR number LIKE '%-%';
Similarly for the next criterion -
trim(sc.NAME) = 'johndoe'
The name should be trimmed before being stored in the database so there is no need to trim it when searching it. Update already stored names to trim whitespace -
UPDATE syn_client
SET NAME = TRIM(NAME)
WHERE NAME LIKE ' %' OR NAME LIKE '% ';
Changing sp.HOSPITAL_ID = 'HOSPITALID999999' to sc.HOSPITAL_ID = 'HOSPITALID999999' will allow for the use of a composite index on syn_client (HOSPITAL_ID, name) assuming you drop the TRIM() from the previously discussed criterion.
The sorting in your sub-query for weight might be wrong -
order by DATE, TIME desc limit 1
presumably you want the most recent weight -
order by `DATE` desc, `TIME` desc limit 1
/* OR */
order by CONCAT(`DATE`, ' ', `TIME`) desc limit 1
order by DATE, TIME desc -- really? That's equivalent to date ASC, time DESC. If you want "newest first", then ORDER BY date DESC, time DESC. Furthermore, it is usually bad practice and clumsy to code when you have DATE and TIME in separate columns. Is there a strong reason for storing them separately? It is reasonably easy to split them apart in a SELECT.
Similarly, cleanse NUMBER and NAME when inserting.
This will make the first subquery much faster:
syn_vital needs INDEX(hostital_id, pet_id, date, time, BW)
LIKE with a leading wildcard (%) is slow, but you probably cannot avoid it in this case.
LOWER(replace(sp.SEX, ' ', '')) -- Cleanse the input during INSERT, not on output!.
LOWER(...) -- With a suitable COLLATION (eg, the default), calling LOWER is unnecessary.
Some of these 'composite' INDEXes may be useful:
ss: INDEX(HOSPITAL_ID, SPECIES_ID, NAME)
st: INDEX(HOSPITAL_ID, CLIENT_ID, NUMBER)
sp: INDEX(HOSPITAL_ID, PET_ID)
What table is TEL_DEFAULT in?
You may want to:
Create index on syn_client(hospital_id, name --,tel_default?)
Create index on syn_tel(hospital_id, client_id, number)
Create index on syn_pet(hospital_id, family_id, state)
Create index on syn_species(hospital_id, species_id)
Change your query to:
SELECT ...
FROM syn_client sc
INNER JOIN syn_tel st ON sc.hospital_id = st.hospital_id AND sc.client_id = st.client_id
INNER JOIN syn_pet sp ON sc.hospital_id = sp.hospital_id AND sc.family_id = sp.family_id AND sp.state = 0
INNER JOIN syn_species ss ON sp.hospital_id = ss.hospital_id AND sp.species_id = ss.species_id
WHERE st.number IN ('01099999999', '01-099-999-999', 'ALL_OTHERS_FORMAT_YOU_ACCEPTS...')
AND trim(sc.name) = 'johndoe' --sc.name = 'johndoe' with standardize data input
AND sc.hospital_id = 'HOSPITALID999999' --not sp.hospital_id
ORDER BY tel_default DESC;
I know that I can combine multiple columns in mysql by doing:
SELECT CONCAT(zipcode, ' - ', city, ', ', state) FROM Table;
However, if one of the fields is NULL then the entire value will be NULL. So to prevent this from happening I am doing:
SELECT CONCAT(zipcode, ' - ', COALESE(city,''), ', ', COALESCE(state,'')) FROM Table;
However, there still can be situation where the result will look something like this:
zipcode-, ,
Is there a way in MySQL to only have to comma and the hyphen if the next columns are not NULL?
There is actually a native function that will do this called Concat with Separator (concat_ws)!
Specifically, it seems that what you would need is:
SELECT CONCAT_WS(' - ',zipcode, NULLIF(CONCAT_WS(', ',city,state),'')) FROM TABLE;
This should account for all of the null cases you allude to.
However, it is important to note that a blank string ('') is different than a NULL. If you want to address this in the state/city logic you would add a second NULLIF check inside the second CONCAT_ws for the case where a city or a state would be blank strings. This will depend on the database's regard for the blank field and whether you are entering true NULLS into your database or checking the integrity of the blank data before you use it. Something like the following might be slightly more robust:
SELECT CONCAT_WS(' - ', zipcode, NULLIF(CONCAT_WS(', ', NULLIF(city, ''), NULLIF(state, '')), '')) FROM TABLE;
For more, check out the native documentation on concat_ws() here:
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_concat-ws
I think you need something like this
Set #zipcode := '12345';
Set #city := NULL;
Set #state := NULL;
SELECT CONCAT(#zipcode, ' - ', COALESCE(#city,''), ', ', COALESCE(#state,''));
Result: 12345 - ,
SELECT CONCAT(#zipcode, IF(#city is NULL,'', CONCAT(' - ', #city)), IF(#state is NULL,'', CONCAT(', ',#state)))
Result 12345
You need to make the output of the separators conditional on the following values being non-NULL. For example:
SELECT CONCAT(zipcode,
CASE WHEN city IS NOT NULL OR state IS NOT NULL THEN ' - '
ELSE ''
END,
COALESCE(city, ''),
CASE WHEN city IS NOT NULL AND state IS NOT NULL THEN ', '
ELSE ''
END,
COALESCE(state, '')
) AS address
FROM `Table``
Output (for my demo)
address
12345 - San Francisco, CA
67890 - Los Angeles
34567 - AL
87654
Demo on dbfiddle
I just built this new conditional query for pulling either a first_name AND last_name OR company_name based on the display_as value:
Select If(`display_as` = 'individual',
CONCAT(first_name, ' ', last_name)
,`company_name`) as name FROM `{$this->table}` WHERE `unique_id` = ? LIMIT 1
The problem is, if the user has a first_name value only and no value for last_name, nothing is returned at all.
How can I fix this?
use this query instead.
$sql = "Select If(`display_as` = 'individual',
CONCAT(IFNULL(first_name, ''), ' ', IFNULL(last_name, ''))
,`company_name`) as name FROM `{$this->table}` WHERE `unique_id` = ? LIMIT 1";
try this one:
Select
If( `display_as` = 'individual',
CONCAT(coalesce(first_name, ''), ' ', coalesce(last_name, ''))
,`company_name`) as name
FROM `{$this->table}`
WHERE `unique_id` = ?
LIMIT 1
I would recommend writing this as:
select (case when display_as = 'individual'
then concat_ws(' ', first_name, last_name)
else company_name
end) as name
from `{$this->table}`
where unique_id = ?
limit 1; -- probably not needed
Notes:
case is the standard SQL construct for conditional logic. if() is a bespoke MySQL extension.
concat_ws() elegantly handles NULL values in the names. It simply ignores the the value rather than returning NULL.
Backticks are not required everywhere. They just make the query harder to write and read.
If your unique_id is really unique, you don't need LIMIT 1.
How i can get value like this in a variable 'TFSEP-2019','TFjul-2018','TFJun-2018' without spaces.
SELECT s.house, s.grade, s.homeroom AS Campus
FROM student s,fees_jrl f
WHERE s.studnum = f.studnum AND
f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
GROUP BY s.house
I am getting like this ('TFJun-2018 TFJUL-2018 TFSEP-2019') but I want like ('TFSEP-2019','TFjul-2018','TFJun-2018') please help
You may refer the following statements with like operators, whenever you need quotes('), use doubled-quotes('') :
create table tab ( Id int, expression varchar(100));
insert into tab values(1 ,'TFJun-2018 TFJUL-2018 TFSEP-2019');
insert into tab values(2,'''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''');
select * from tab where expression like '''TFJun-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
or
select * from tab where expression like '''TFJun-2018'',''TFJUL-2018'',''TFSEP-2019''%';
Id expression
2 'TFJun-2018','TFJUL-2018','TFSEP-2019'
Rextester Demo
You can use like for strings
% means any chars, _ means one char
So doing
F.name like %2018%
Will give you all of 2018..
use explicit join and concat
SELECT
concat( concat( concat( concat("'",s.house),"'"),concat( concat("'",s.grade),"'")),
concat( concat("'",s.homeroom),"'"))
FROM student s join fees_jrl f
on s.studnum = f.studnum
where f.name IN (' TFJun-2018 TFJul-2018 ') AND
f.trans_type= 'chg' AND
f.paid_id is NULL AND s.house LIKE '%'
I think you dont need group by as you have no aggregation function
I want to validate the data in MYSQL table. Table has 4 fields :
Firstname
Middlename
Lastname
Fullname
I want to compare if CONCAT(firstname, ' ', LASTNAME), matches Fullname
Here is the command I am using :
select * from user_info where CONCAT(firstname, ' ', lastname)
like CONCAT('%', fullname, '%')
However, this is not working. But the following command works :
select * from user_info where CONCAT(firstname, ' ', lastname)
like '%JOHN DOE%'
What could be the issue with the MySQL command ?
Your newly attached data confirms what I suspected, namely that the full name is not necessarily composes simply of the first and last name, but include the middle name, or might even be missing any of the three components. One option here would be to assert that each component present does appear somewhere inside the full name, in the correct order, e.g.
SELECT *
FROM yourTable
WHERE
fullname REGEXP
CONCAT(
CASE WHEN firstname IS NOT NULL
THEN CONCAT('[[:<:]]', COALESCE(firstname, ''), '[[:>:]]')
ELSE '' END,
'.*',
CASE WHEN middlename IS NOT NULL
THEN CONCAT('[[:<:]]', COALESCE(middlename, ''), '[[:>:]]')
ELSE '' END,
'.*',
CASE WHEN lastname IS NOT NULL
THEN CONCAT('[[:<:]]', COALESCE(lastname, ''), '[[:>:]]')
ELSE '' END);
Demo
You need to change order of condition in your code :-
select * from table1 where fullname
like CONCAT('%', trim(CONCAT(firstname,' ',middlename,' ',lastname)) , '%')
SQL Fiddle