Query MySQL database find missing email addresses - mysql

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)

Related

mySQL query: finding values in backup table

I have 2 tables: roles and roles_backup.
due to faulty backend code, values from the field role have been set to NULL (only with certain records).
what i need to acomplish: finding all records from the backup table where role is set in order to get them back.
what is the best method of querying this?
i've tried the following but it doesn't seem to work:
SELECT * FROM roles WHERE (role IS NULL OR role='') UNION
SELECT * FROM roles_backup WHERE (role <>'')
Assuming these records have an ID, you could do something like this to restore your roles from the roles_backup table:
UPDATE roles
SET role = ( SELECT role FROM roles_backup WHERE id = roles.id )
WHERE role IS NULL

MySQL if result null change query

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

How to use data in same table on UPDATE query?

update accounts set password=(select password from accounts where name='joongsu')
where id=(select accountid from characters where name='Nobless')
it doesn't work with error message "You can't specify target table 'accounts' for update in FROM clause"
Why doesn't it work? select queries in above only return 1 row.
Perhaps you should try this one:
UPDATE accounts
SET accounts.password =
(
SELECT something.password
FROM (SELECT * FROM accounts) AS something
WHERE something.name='joongsu'
)
WHERE accounts.id=(SELECT accountid FROM characters WHERE name='Nobless');
It's a hack, but I tested it and it works on my test data. For some reason MySQL doesn't allow using the same table in inner queries as the one being updated.
UPDATE
accounts AS account_to_be_updated
JOIN
characters
ON characters.accountid = account_to_be_updated.id
AND characters.name = 'Nobless'
CROSS JOIN
( SELECT password
FROM accounts
WHERE name = 'joongsu'
) AS existing_account
SET
account_to_be_updated.password = existing_account.password ;
Is this what you looking out for?
;with CTE as
(
select password from accounts where name='joongsu' limit 1
)
update accounts set password= CTE.password
where id in
(select accountid from characters where name='Nobless')

SQL Preferred column

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.

MySQL: Finding duplicates across multiple fields

Background: my employer has a database powered by a really old version of MySQL (3.23). I have been asked to find duplicate serial numbers and MAC addresses in the database.
I was able to find the duplicate serial numbers, but since this version of MySQL doesn't support subqueries, I had to resort to using a temporary table. These are the two SQL statements I ended up using:
CREATE TEMPORARY TABLE IF NOT EXISTS Inventory_Duplicate_Serials
SELECT Serial
FROM Inventory
WHERE Serial IS NOT NULL
GROUP BY Serial
HAVING COUNT(Serial) > 1
SELECT DeviceName, Model, Inventory.Serial
FROM Inventory
INNER JOIN Inventory_Duplicate_Serials
ON Inventory.Serial = Inventory_Duplicate_Serials.Serial
ORDER BY Serial
Now I need to find the duplicate MAC addresses. The problem is the "Inventory" table has three MAC address fields (MAC, MAC2, and MAC3). So, for example, if the value of an item's "MAC" field is the same as the value of another item's "MAC2" field, I need to know about it. How do I go about doing this? Thank you for your time.
UPDATE: Solved. I ended up creating two temporary tables (Inventory_All_MACs and Inventory_Duplicate_MACs). These are the five queries:
CREATE TEMPORARY TABLE IF NOT EXISTS Inventory_All_MACs
SELECT MAC
FROM Inventory
WHERE MAC != ''
CREATE TEMPORARY TABLE IF NOT EXISTS Inventory_All_MACs
SELECT MAC2 AS MAC
FROM Inventory
WHERE MAC2 != ''
CREATE TEMPORARY TABLE IF NOT EXISTS Inventory_All_MACs
SELECT MAC3 AS MAC
FROM Inventory
WHERE MAC3 != ''
CREATE TEMPORARY TABLE IF NOT EXISTS Inventory_Duplicate_MACs
SELECT MAC
FROM Inventory_All_MACs
GROUP BY MAC
HAVING COUNT(MAC) > 1
SELECT DeviceName, Model, Inventory_Duplicate_MACs.MAC AS DuplicateMAC, Inventory.MAC, MAC2, MAC3
FROM Inventory_Duplicate_MACs
INNER JOIN Inventory
ON Inventory.MAC = Inventory_Duplicate_MACs.MAC
OR Inventory.MAC2 = Inventory_Duplicate_MACs.MAC
OR Inventory.MAC3 = Inventory_Duplicate_MACs.MAC
ORDER BY Inventory_Duplicate_MACs.MAC, DeviceName, Model
Thanks everybody!
A 'simple' solution that comes to mind is to create a second temporary table that lists all MAC addresses in one column, so you would need to create three entries for one entry from the first temporary table.
CREATE TEMPORARY TABLE IF NOT EXISTS Inventory_Mac
SELECT Mac
FROM Inventory
INSERT INTO Inventory_Mac
SELECT Mac2
FROM Inventory
INSERT INTO Inventory_Mac
SELECT Mac3
FROM Inventory
CREATE TEMPORARY TABLE IF NOT EXISTS Inventory_Duplicate_Mac
SELECT Mac, COUNT(*) AS cnt
FROM Inventory_Mac
GROUP BY Mac
HAVING COUNT(*) > 1
SELECT DeviceName, Model, im.Mac, i.Mac, i.Mac2, i.Mac3
FROM Inventory_Duplicate_Mac AS im
JOIN Inventory AS i
ON i.Mac = im.Mac
OR i.Mac2 = im.Mac
OR i.Mac3 = im.Mac
ORDER BY im.Mac
Not 100% sure on this answer but it could be worth a try using LEFT JOINS e.g:
SELECT address1
FROM addresses
LEFT JOIN Inventory_Duplicate_Addresses ad1
ON Addresses.MAC = ad1.mac
LEFT JOIN Inventory_Duplicate_Addresses ad2
ON Addresses.MAC = ad2.mac2
LEFT JOIN Inventory_Duplicate_Addresses ad3
ON Addresses.MAC = ad3.mac3