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"
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)
Table Values
CNAME
Firstname
Amount
Postalcode
Lastname
Accountnumber
REQUIRED O/P
CNAME
'Firstname'
'Amount'
'Postalcode'
'Lastname'
'Accountnumber'
In mysql you can use the function concat():
SELECT CONCAT("'", CNAME, "'") FROM yourTable
In oracle you can use the same function as above concat() or the concatenation operator:
SELECT '''' || CNAME || '''' FROM yourTable;
SELECT CONCAT('''', CNAME, '''') FROM yourTable;
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
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.