what if I have two strings: "123" and "abc". I want to select username if there's username "123" then choose it, if not found (null) then select username where "abc"
I have a table called USERS, this table responsibility with workflow engine account. I want to show columns in USERS:
username
email
usr_firstname
usr_lastname
I am using concat to merge column 3 and 4 with space between it. In the office, there are 2 types of employee:
origin/internal employee
outsource/partner employee
Origin employee login into every system using LDAP (FirstName.LastName), but outsource or partner employee login individually just for our workflow engine using employee identity number.
In this case, if I use something like:
Where username = 'employeenumber' or username = 'LDAPacc' the result is both account (used and unused for outsource) they appear. I want to show just 1 rows and 1 query but it's work with internal or even outsource (they will got data correctly for outsource).
You can use like this query;
SELECT *
FROM TABLE
WHERE username IN ('123', 'abc')
AND (username='123' OR NOT EXISTS(SELECT * FROM TABLE WHERE username='abc'))
You could use COALESCE.
COALESCE selects the first non null value out of the ones supplied.
So you could use....
SELECT COALESCE(String_123, string_ABC);
If string_123 has a value it will select that, otherwise it will select string_ABC unless of course they are both null.
So to be safe include a default value.......
SELECT COALESCE(String_123, string_ABC, string_Default);
I've found when I tested my logic to mysql tryit editor by w3schools and It's worked properly what I need. Here's my query:
SELECT * FROM Customers WHERE CustomerID = 'zz' OR (NOT EXISTS (SELECT * FROM Customers WHERE CustomerID = 'zz') AND CustomerID = '3')
let's say CustomerID is equivalent to my username column, then I tried to swap 'zz' and '3' value and it's still works. I hope there's more simple query than this
Related
Here is the rule:
When comparing userId, only search userId starting with 'AB' and its matching duplicates (except 'AB'). Then get a list of "unique userId" by only returning above duplicated userId that is having 'AB' at the beginning.
For returned duplicated string starting with 'AB', we need to make sure there is "duplicate"; otherwise, we should not return 0 record
I know it sounds confusing, please see example below:
Table UserName with ten records, and its userId fields (10 records) are:
ABC1234
C1234
C12345
BC12345
BBC1234
ABF1235
F1235
ABY1236
BCD3456
D3456
Desired Result after running query:
ABC1234
ABF1235
Please note: Although ABY1236 starts with 'AB', this record should not be returned in output, since it doesn't have a "duplicate" match like Y1236 (ignoring first two character, 'AB').
I have a sample query below, but it only returned duplicated record NOT starting with 'AB', also it will return ABY1236.
SELECT distinct substr(userId , -(length(userID)-2))
from UserName where userId like 'AB%';
Thanks for the help!
You can use EXISTS to check if there is a userId that is equal to the right part of "AB.." starting from the 3d char:
select u.userId from UserName u
where
u.userId like 'AB_%'
and
exists (
select 1 from UserName where userId = substr(u.userId, 3)
)
You could try using a selct join for check only the matching result
SELECT substr(a.userId , -(length(a.userID)-2))
from UserName a
INNER JOIN UserName b ON a.substr(a.userId , -(length(a.userID)-2)) = b.UserId
AND userId like 'AB%'
I have a master table called client names. The key field is clientid and is a char 36. I have an address table. I need to update the address table's clientid fied, define the same as above, with the value from clientnames. My SQL runs but no rows are update. My first SQL is:
UPDATE address
SET clientid =
(
SELECT c.clientid FROM clientnames c
JOINrempAddress ra ON c.lastname = ra.lastname AND c.firstname = ra.firstname
)
The inner select returns the values I expect.
I have even tried:
UPDATE address SET clientid = 'AB3'.
Still no rows are updated. What am I doing wrong? Is 1and1 MySQL different and thus has different syntax?
Thank you.
My issue was that there were trailing spaces in the first name on one of the tables. When I used the TRIM function it worked as expected.
I want to use the result from a select query as the input value for another select query.
Here's an example:
SET #name = 'Tony';
select * from People where name=#name;
Returns a table of 2 columns
name surname
Tony Danza
Tony Bennett
I then want to use the surname of the first record to look up a subscription.
select * from Subscription where user='Danza';
Is it possible to use a placeholder / formula instead of typing in 'Danza' manually? Something like:
select * from Subscription where user=(result from first query);
I have 8 select queries that depend on the result of the previous select queries so I'm not sure if joins are the way to go. i.e. name > surname > subscription > readerID > deviceId > etc
I'm trying to locate records and then delete them. Here are the actual queries that I'm trying to use
SET #readerId=-7256784839031027017; // set manually
select * from Reader where readerId=#readerId;
SET #readersId=788216; // use the previous query's readerId column
select * from Reader_Device where READERS_ID=#readersId;
SET #deviceId=786527; // use the previous query's DEVICES_ID column
select * from Device where id=#deviceId;
SET #subscriptionValue='B1AA9B9FFBAE46918C079CAEC06EDC3B'; // use the previous query's deviceId column
select * from Subscription_attributes where KEY0='deviceId' and value=#subscriptionValue;
SET #subscriptionId=786618; // use the previous query's SUBSCRIPTION_ID column
delete from Installation where DEVICE_REF=#deviceId;
delete from Reader_Device where DEVICES_ID=#deviceId;
delete from Device where id=#deviceId;
delete from Subscription_attributes where KEY0='deviceId' and value=#subscriptionValue;
delete from Subscription_attributes where SUBSCRIPTION_ID=#subscriptionId;
delete from Subscription where id=#subscriptionId;
delete from Reader where readerId=#readerId;
by join :
select s.* from subscription s join people p on s.user = p.surname
where p.name = #name /* modify as per your requirement */
by in clause :
select * from subscription
where user in(select surname from people where name = #name)
I hope this will help.
I have field that has value kind of store878 . I would like to have 878 from select statement. How do I get that numbers from select statement
select
store,
address
from
detail,
store_number
where
store (here i would like to have number) = store_mumber.id
I haven't tested this to make sure is works:
SELECT store, address
FROM detail d
INNER JOIN store_number s
ON CAST(SUBSTRING(d.store, LOCATE('%[0-9]%', d.store)) AS int) = s.id
But you should really consider changing the structure of your database.
I have a MySQL table that keeps details about people, for example:
Name
Address
WorkEmail
HomeEmail
Preferred Email ('WorkEmail' or 'HomeEmail')
I want to select their preferred email only. Is there a neat way to do this using SQL? or will I just need to do this after I pull out the data?
A simple case statement should do the trick:
SELECT
Name,
CASE WHEN PreferredEmail = 'WorkEmail' THEN WorkEmail ELSE HomeEmail END AS Email
FROM
MyTable
(Select WorkEmail from MyTable where preferredEmail = 'WorkEmail')
Union
(Select HomeEmail from MyTable where preferredEmail = 'HomeEmail')
select IF(PreferredEmail = 'WorkEmail', WorkEmail, HomeEmail) AS Email
Why do you need a list of more than one email? If you are collecting multiple email address and you may at sometime want to collect more than one you could always create a secondary table that would associate the user with all of their possible email addresses and have a second third column that could be a flag to signify the Primary email.
Main Table
- UID
- Name
- Address
Email table
- UID
- Email address
- Primary
Then you could just do a query for where Primary is flagged and join to the other on the Unique ID.