I have the following query:
SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user
How would I do the equivalent of:
SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user
**WHERE full_name IS NOT NULL**
(which produces a Unknown column 'full_name' in 'where clause')
Two ways around this:
SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user
WHERE first_name IS NOT NULL and last_name is not null
or,
select * from (
SELECT concat(first_name, ' ', last_name) as full_name FROM auth_user)
where full_name is not null
SELECT concat(IF(first_name IS NOT NULL, first_name, ''), ' ', IF(last_name IS NOT NULL, last_name, '')) as full_name FROM auth_user;
The difference between using IS NOT NULL in the where clause and using it in the IF clause is that you the where clause will eliminate (filter) the rows with null values, while the if clause will get all the rows but with a full_name that will not be "Unknown" it will just be missing the null components
Related
I would like rows returned in a MySQL query to be sorted alphabetically by surname for which I have an SQL query like:
SELECT
id,
substring_index(name, ' ', -1) as surname
FROM
my_table
ORDER BY
surname asc
However, some names have parenthesis to denote some special circumstance such as: Laura Angel (retired)
How can I modify my SQL above to ignore the parenthesised text, to sort by surname alphabetically?
Try with nested replaces to remove the parentheses.
SELECT
id,
substring_index(name, ' ', -1) as surname
ORDER BY
REPLACE( REPLACE( surname , '(' , '') , ')' , '') ASC;
Test and modify according to you version of SQL.
Not tested.
You can use this solution:
SELECT id,
substring_index(rtrim(substring_index(name, '(', 1)), ' ', -1) as surname
FROM test.test
ORDER BY
surname asc;
this works
SELECT USERID, FNAME, LNAME FROM USERS WHERE CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE(?, ' ', '%'), '%', '%')"
but i want to addon this
TBL USERS
USERID FNAME LNAME EMAIL
1 JONE DEO tes#doe.com
2 JANE DEO tes1#deo.com
3 Jow DEO tdb#deo.com
TBL MSG
MSGID RECEIVED READ
1 YES YES
2 NO NO
3 NO NO
THIS IS MY TRY
SELECT USERID, FNAME, LNAME FROM USERS WHERE CONCAT(FNAME, ', ', LNAME, ', ', EMAIL) LIKE CONCAT('%', REPLACE(?, ' ', '%'), '%', '%' WHERE NOT EXISTS ( SELECT * FROM MSG WHERE
RECEIVED = 'YES' AND READ = 'YES' ))"
so i want to select only users which not exits in RECEIVED(YES) AND READ (YES)
I have three columns in my table: firstName, lastName and jobTitle.
I want to concat firstName and lastName as Fullname then concat Fullname and jobTitle.
How can I do that?
You would do:
select concat_ws(' ', firstName, lastName) as Name,
concat_ws(' ', firstName, lastName, jobTitle) as NameTitle
You cannot re-use a column alias in the same select, so you have to repeat the expression.
EDIT:
If you want one column of that form, then perhaps:
select concat(firstname, ' ', lastname, ', ', jobtitle)
I don't believe you can use a column alias for this, you'd have to simply select all three of the columns at once.
CONCAT(firstName, lastName, jobTItle) as nameJob
Maybe like that
SELECT CONCAT(firstname, ' ', lastname) as fullname,CONCAT(fullname,'-',jobTitle) as info
FROM yourTable
another example
SELECT CONCAT(firstname, ' ', lastname) as fullname,CONCAT(fullname,'-',jobTitle) as info
FROM yourTable WHERE CONCAT(firstname, ' ', lastname) = "Bob Marley"
I have the Following Query:
SELECT BUSINESS_NAME, 'KEYWORD', REPLACE(BUSINESS_NAME, ' ', '-')
FROM clearindia.business b
LEFT OUTER JOIN `clearindia`.`keywords_master` km ON km.KEYWORD_TEXT = b.BUSINESS_NAME
WHERE km.KEYWORD_TEXT IS NULL
AND b.business_name='Dey Radio Service'
GROUP BY BUSINESS_NAME
It gives me the following results:
# BUSINESS_NAME, KEYWORD, REPLACE(BUSINESS_NAME, ' ', '-')
'Dey Radio service', 'KEYWORD', 'Dey-Radio service'
REPLACE(BUSINESS_NAME, ' ', '-') is not working correctly and does not replace the second space with a '-'. Why is that?
Please Note: BUSINESS_NAME has a collation of utf_unicode_ci.
I thought this would be easy... maybe not. I have a table with 'fullname' and I want to split the first name and last name into 2 columns (fname and lname).
The following syntax gives me the data I want:
SELECT
`fullname` ,
SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1) AS fname,
SUBSTRING_INDEX(`fullname` ,' ', 1)AS lname
FROM MyTable
... but how do I then take the 'fname' and 'lname' fields and save them to separate columns in the same table?
[Example data -- If the persons name is John Michael Jones, the 'fullname' field looks like this: JONES JOHN MICHAEL ]
You can just update your table and set the columns to the substrings of the fullname
update MyTable
set fname = SUBSTRING_INDEX(SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
If you want to perform the update on the same table, you can use the following:
update mytable
set fname = SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
See SQL Fiddle with Demo
you want to update into first name ans last name value then you need to used update :
update MyTable
set fname = SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1),
lname = SUBSTRING_INDEX(`fullname` ,' ', 1)
and if you want to set in upper case then user upper function as well as
update MyTable
set fname = upper(SUBSTRING_INDEX( SUBSTRING_INDEX(`fullname` ,' ', 2) ,' ' ,-1)),
lname = upper(SUBSTRING_INDEX(`fullname` ,' ', 1))