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.
Related
I am trying to extract email addresses from given list, that not persists in MySql database. My query:
SELECT *
FROM users
WHERE `user_email` IN ('myemail#email.com', 'my2email#email.com', 'my3email#email.com')
First two email addresses are in database, but the last one is not. My target is to print only emails that are NOT in database. How is that possible?
SELECT *
FROM (SELECT 'myemail#email.com' user_email
UNION ALL
SELECT 'my2email#email.com'
UNION ALL
SELECT 'my3email#email.com') emails_to_check_for
LEFT JOIN users USING (user_email)
WHERE users.user_email IS NULL;
create a temporary table and insert all the email ids that you want to check
create table check_emailid(email_id varchar(255))
insert into check_emailid(email_id )
values('myemail#email.com')
values('my2email#email.com')
values('my3email#email.com')
select *
from check_emailid
where email_id not in (SELECT user_email
FROM users)
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
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'm not sure the best way to describe this,
I have a table that contains reported usernames from my users
'Name' 'reason' 'gender' 'date'
joe FAKE male 10/10/2013
the gender column tells me which table the username resides, in this case 'male_users'
is there some way/function to take the name column and the gender and go to the corresponding table and delete that username, without me having to manually copy the name - > change table -> search for usernames like 'joe' -> delete -> start over
I hope this makes sense!
Thanks
You can use the multiple-table DELETE syntax together with some outer joins as follows:
DELETE m, f
FROM reported_users r
LEFT JOIN male_users m ON m.username = r.Name AND r.gender = 'male'
LEFT JOIN female_users f ON f.username = r.Name AND r.gender = 'female'
But really, why have the separate male_users and female_users tables? Why not have a single table with a column containing the user's sex?
The best way to do it will be to use two commands in row:
DELETE FROM male_users WHERE name IN(
SELECT name FROM users WHERE <logic> AND gender='male'
);
DELETE FROM female_users WHERE name IN(
SELECT name FROM users WHERE <logic> AND gender='female'
);
Solved it myself in the end, the answers above are also correct, however i chose to create a php script to read the values and display a button beside each entry to allow me to delete from the table with a simple
'DELETE FROM $gender_of_table WHERE username = $username'
I am using the following query to select emails that are duplicates:
SELECT email
FROM contacts
WHERE active = 1
GROUP BY email
HAVING ( COUNT(email) > 1)
In the case above, if two users have the same email, that query will show them. However, there is another email field named email2, what can I do so that I compare the count among many email fields? (So, for example, if a user has email equal to email2 of another user, they will be considered duplicates)
Thank you!
SELECT tmp.email FROM (
SELECT email as email FROM contacts WHERE active = 1
UNION ALL
SELECT email2 as email FROM contacts WHERE email != email2 AND active = 1
) as tmp
GROUP by tmp.email
HAVING COUNT(tmp.email) > 1
P.S : my syntax could be wrong, but the idea should be something like this.
SELECT email
FROM contacts
WHERE (email = email2) AND (active = 1)
if I'm interpreting you correctly. Or do you mean that some user records may have "user#example.com" in the email field in one record, and the same address in the email2 field in another record? that'd be
SELECT primary.email, primary.id, secondary.id
FROM contacts AS primary
INNER JOIN contacts AS secondary ON (primary.email = secondary.email2)