MySQL concat with not exits condition - mysql

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)

Related

SQL Query to display unique records based on certain conditions

[I have gone through a large number of questions before posting this question.]
I have a table which contains 4 fields. It is ClientId, ClientName,ClientAddress, ClientCity.
Now, I have an autocomplete textbox control where I need to fetch & display client name.
The problem is that in our database we have same client from the same city from different address.
Now the requirement given to me is that the user should be able to see "ClientName" or "ClientName + ClientCity" or "ClientName+ClientCity+ClientAddress" to make it easy for user to select the client.
It means that I need to add a column to the query till it makes it unique.
I am sure there must be some simple solution to this which I am not getting for past 2 days now.
As shown in below sample data, If I show only "D" as a client name to the end user, he will be confused as in which client "D" he has to select. So we need to concatenate city and address to make it unique.
I am expecting an output as below.
You can use COUNT() OVER() for this:
;WITH CTE AS(
SELECT *,
ByName = COUNT(*) OVER(PARTITION BY ClientName),
ByCity = COUNT(*) OVER(PARTITION BY ClientName,ClientCity)
FROM Client
)
SELECT
CASE
WHEN ByName = 1 AND ByCity = 1 THEN ClientName
WHEN ByName > 1 AND ByCity = 1 THEN ClientName + ', ' + ClientCity
WHEN ByName > 1 AND ByCity > 1 THEN ClientName + ', ' + ClientCity + ', ' + ClientAddress
END
FROM CTE
ORDER BY ClientID
RESULT
Client
--------------------------------------------------------
A
B
C, New York
D, London, LSE Houghton Streen London WC2A 2AE
D, London, Hard Rock Cafe London 150 Old Park Lane
F
C, Paris
See SQL Fiddle.
If you are using SQL Server, you can try the string concatenation using "+" operator as follows
select
ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) Client
from client
The second concatenation is built using SQL CONCAT() funtion will work on SQL Server 2012 and later
For the conditional data following SELECT statement can help,
select
-- ClientName + ', ' + ClientCity + ', ' + ClientAddress as ClientData,
-- Concat(ClientName, ', ', ClientCity, ', ', ClientAddress) ClientDtata2,
client =
case when count(*) over (partition by ClientName) = 1 then ClientName
else
case when count(*) over (partition by ClientName, ClientCity) = 1 then CONCAT(ClientName, ', ' , ClientCity)
else Concat(ClientName, ', ', ClientCity, ', ', ClientAddress)
end
end
from client
TRY THIS,
Declare #t table (clientid int identity(1,1),clientname varchar(50),clientCITY varchar(50)
,clientaddress varchar(200))
insert into #t values ('A','PARIS','DFSDFSDF'), ('C','NEW YORK','DFSDFSDF')
,('C','PARIS','WEQWEQWE'),('D','LONDON','QWE342'),('D','LONDON','21DXCXZC')
;With CTE as
(select *,ROW_NUMBER()over(partition by clientname order by clientid)rn
,ROW_NUMBER()over(partition by clientname,ClientCity order by clientid)rn1
from #T A
)
--select * from cte
select clientname+','+ clientCITY
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 AND RN1<=1)
UNION ALL
select clientname+','+ clientaddress
from cte A WHERE
EXISTS(SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN1>1)
UNION ALL
select clientname
from cte A WHERE not
EXISTS (SELECT CLIENTID FROM CTE WHERE clientname=A.clientname AND RN>1 )

Use concat with as inside concat funcation in MySQL

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"

MySQL split single column

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

How to remove NULL results on concatenation

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

Insert data from an existing/non-existing column to another from a calculated mysql result?

Using the function to split strings: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX( fullname, ' ', 1 ) , ' ', -1 ) AS firstname,
SUBSTRING_INDEX( SUBSTRING_INDEX( fullname, ' ', 2 ) , ' ', -1 ) AS lastname
FROM users;
I get the following resuts:
firstname | lastname |
john doe
jane doe
I have the following questions:
1.)How can I insert the values on columns that already exist under the same table?
2.)How can I insert the values on columns that don't exist under the same table?
Of course, the two columns being firstname | lastname
To insert these values:
INSERT INTO users (firstname, lastname)
SELECT SUBSTRING_INDEX( SUBSTRING_INDEX( fullname, ' ', 1 ) , ' ', -1 ) AS firstname,
SUBSTRING_INDEX( SUBSTRING_INDEX( fullname, ' ', 2 ) , ' ', -1 ) AS lastname
FROM users;
To update existing values, for the same row:
update users
set firstname = SUBSTRING_INDEX( SUBSTRING_INDEX(fullname, ' ', 1 ) , ' ', -1 ),
lastname = SUBSTRING_INDEX( SUBSTRING_INDEX(fullname, ' ', 2 ) , ' ', -1 )
To update existing values, but a different row:
update users users_a, users users_b
set users_b.firstname = SUBSTRING_INDEX( SUBSTRING_INDEX( users_a.fullname, ' ', 1 ) , ' ', -1 ),
users_b.lastname = SUBSTRING_INDEX( SUBSTRING_INDEX( users_a.fullname, ' ', 2 ) , ' ', -1 )
where users_a.??? = users_b.??? /* join condition as appropriate */