I'm trying to compile this in my mind.. i have a table with firstname and lastname fields
and i have a string like "Bob Jones" or "Bob Michael Jones" and several others.
the thing is, i have for example
Bob in firstname, and
Michael Jones in lastname
so i'm trying to
SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast
FROM users
WHERE firstlast = "Bob Michael Jones"
but it says unknown column "firstlast".. can anyone help please ?
The aliases you give are for the output of the query - they are not available within the query itself.
You can either repeat the expression:
SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast
FROM users
WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones"
or wrap the query
SELECT * FROM (
SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast
FROM users) base
WHERE firstLast = "Bob Michael Jones"
Try this:
SELECT *
FROM (
SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast
FROM users
) a
WHERE firstlast = "Bob Michael Jones"
SELECT needefield, CONCAT(firstname, ' ',lastname) as firstlast
FROM users
WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones"
Use CONCAT_WS().
SELECT CONCAT_WS(' ',firstname,lastname) as firstlast FROM users
WHERE firstlast = "Bob Michael Jones";
The first argument is the separator for the rest of the arguments.
Try:
SELECT neededfield, CONCAT(firstname, ' ', lastname) as firstlast
FROM users
WHERE CONCAT(firstname, ' ', lastname) = "Bob Michael Jones"
Your alias firstlast is not available in the where clause of the query unless you do the query as a sub-select.
There is an alternative to repeating the CONCAT expression or using subqueries. You can make use of the HAVING clause, which recognizes column aliases.
SELECT
neededfield, CONCAT(firstname, ' ', lastname) AS firstlast
FROM
users
HAVING firstlast = "Bob Michael Jones"
Here is a working SQL Fiddle.
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 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))
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