SQL query to find where ID does not exist in another column - mysql

I have a table: Accounts
+-----------+-----------------+------+
| AccountNo | ParentAccountNo | name |
+-----------+-----------------+------+
| 1 | null | ABC |
| 2 | 1 | ABCD |
| 3 | 1 | CDE |
| 4 | 2 | DEF |
| 5 | null | GHI |
| 6 | 3 | MNO |
| 7 | 5 | JKL |
+-----------+-----------------+------+
I need to get results where AccountNo does not exist in ParentAccountNo.
For example, based on the data above, the results I expect are:
+-----------+-----------------+------+
| AccountNo | ParentAccountNo | name |
+-----------+-----------------+------+
| 4 | 2 | DEF |
| 6 | 3 | MNO |
| 7 | 5 | JKL |
+-----------+-----------------+------+
Will accept answer for MySQL or SQL-Server.

By joining the table to itself with a LEFT JOIN on the AccountNo = ParentAccountNo, you can then filter out the rows where there were no matching ID's.
WITH Accounts AS(
SELECT *
FROM (VALUES (1,null,'ABC'),
(2,1,'ABCD'),
(3,1,'CDE'),
(4,2,'DEF'),
(5,null,'GHI'),
(6,3,'MNO'),
(7,5,'JKL')) V(AccountNo,ParentAccountNo,name))
SELECT t1.*
FROM Accounts t1
LEFT JOIN Accounts t2 ON t1.AccountNo = t2.ParentAccountNo
WHERE t2.AccountNo IS NULL
[DEMO HERE]
You were curious about the usage of WITH. . .
Here is an equivalent to my shorthand usage of WITH:
CREATE TABLE Accounts (
AccountNo INT NOT NULL,
ParentAccountNo INT NULL,
Name VARCHAR(200))
INSERT INTO Accounts
SELECT *
FROM (VALUES (1,null,'ABC'),
(2,1,'ABCD'),
(3,1,'CDE'),
(4,2,'DEF'),
(5,null,'GHI'),
(6,3,'MNO'),
(7,5,'JKL')) V(AccountNo,ParentAccountNo,name)

Related

select multiple rows form a table and insert into a different table

I have the following tables..
Passengers:
| Reservation_number | Passport_number | Full_name |
|--------------------|-----------------|-----------|
| 1 | 1 | abc |
| 1 | 2 | def |
| 1 | 3 | ghi |
Booking:
| Booking_id | Reservation_number | Card_number | Price |
|------------|--------------------|-------------|-------|
| 1 | 1 | 6767 | 460 |
Ticket:
| Booking_id | Passport_number | Ticket_number |
|------------|-----------------|---------------|
| | | |
| | | |
From Passengers,i want to select all the Passport_number's for which Reservation_number is 1(here it is all the 3 rows) and then insert each of those Passport_number's into each row of Ticket table. After that, all the rows for Booking_id in Ticket table must be set to 1 since the Booking_id is 1 for Reservation_number=1(this can be seen in Booking table). How can i do it?
Consider the insert ... select syntax.
You can join booking and passengers, and insert the resultset into ticket as follows:
insert into ticket(booking_id, passport_number, ticket_number)
select b.booking_id, b.reservation_number, p.passport_number
from booking b
inner join passengers p on p.reservation_number = b.reservation_number

Show the column not null of two tables in SQL

I would like to join two tables and select from two columns the first one if it is not null, of the other if the first is null. As an example imagine that we have the following tables:
names companies_to_names
-------------------------------- -----------------------------
|id_name | name | nickname | | id | id_name | id_company |
-------------------------------- -----------------------------
| 1 | NULL | manu | | 1 | 1 | 1 |
| 2 | Joe A. | NULL | | 2 | 2 | 1 |
| 3 | Bob B. | NULL | | 3 | 3 | 1 |
| 4 | NULL | alice | | 4 | 4 | 1 |
| 5 | NULL | other | | 5 | 5 | 2 |
-------------------------------- -----------------------------
And we want to show either the name, or the nickname of the guys who work for the company with id=1. Then, I want the following result:
--------------------
|id_name | username|
--------------------
| 1 | manu |
| 2 | Joe A. |
| 3 | Bob B. |
| 4 | alice |
--------------------
I was thinking in SELECT CASE WHEN, but I don't know how to do it. Something like:
SELECT NAMES.id_name CASE username
WHEN NAMES.name IS NULL THEN NAMES.nickname
WHEN NAMES.name IS NOT NULL THEN NAMES.name
END
FROM NAMES INNER JOIN COMPANIES_TO_NAMES ON NAMES.id_name = COMPANIES_TO_NAMES.id_name;
Am I right?
Here is a query that shows you how to solve your problem:
SELECT N.id_name
,IFNULL(N.name, N.nickname) AS [username]
,CASE
WHEN N.name IS NOT NULL THEN 'name'
ELSE 'nickname'
END AS [username_source]
FROM NAMES N
INNER JOIN companies_to_names C ON C.id_name = N.id_name
AND C.id = 1
Hope this will help you.

SQL Insert by from another table through a reference table

I've got three tables in MySQL: wp_usermeta, directory_department, and directory_departmentmemebership
Directory_departmentmembership
| employee_id|department_id|
|:-----------|------------:|
| 1 | 2 |
| 2 | 1 |
| 3 | 3 |
directory_department
| department_id|department_name|
|:----------- |--------------:|
| 1 | Deans |
| 2 | MBA |
| 3 | Fiance |
wp_usermeta has:
| employee_id| meta_key | meta_value |
|:-----------|--------------:|:------------:|
| 1 |department_id | 2 |
| 2 |department_id | 1 |
| 3 |department_id | 3 |
| 1 |department_name| |
| 2 |department_name| |
| 3 |department_name| |
How I can I reference directory_department to insert the correct department_name based off of the department_id value in the meta_value field?
You can use a SQL function to get the right name for each id...
CREATE FUNCTION [dbo].[fn_department](#id INT)
RETURNS VARCHAR(300)
AS
BEGIN
declare #department_name varchar(300)
select #department_name = department_name from directory_department where department_id = #id
RETURN #department_name
END
--And call it like this...
INSERT INTO wp_usermeta (id,department_name)
select id, dbo.fn_department(id) as department_name

MySQL | Update query to populate children

I have a table like
ID | ParentID |Children| Name | Address
----------------------------------------
1 | Null | | Xyz | Home
2 | 1 | | ABC | Home
3 | 2 | | DEF | Home
4 | 3 | | GHI | Home
5 | 3 | | GHI | Home
6 | 1 | | GHI | Home
I need to fill the Children with a query, these are immediate children. For example children for 1 will be 2 and children for 3 will be 4,5
So the table after query should look like
ID | ParentID |Children| Name | Address
----------------------------------------
1 | Null | 2,6 | Xyz | Home
2 | 1 | 3 | ABC | Home
3 | 2 | 4,5 | DEF | Home
4 | 3 | Null | GHI | Home
5 | 3 | Null | GHI | Home
6 | 1 | Null | GHI | Home
I know I can use group_concat function with update query but not getting the exact query. How can I do this with a MySQL query?
You can create a subquery which concatenate the ID for its children and join it with the table itself.
UPDATE tableName a
INNER JOIN
(
SELECT a.ID, GROUP_CONCAT(b.ID) Children
FROM tableName a
INNER JOIN tableName b
ON a.ID = b.ParentID
GROUP BY a.ID
) b ON a.ID = b.ID
SET a.Children = b.Children

Having difficulty with the WHERE IN clause for MySQL

Ok, I realize this may be incredibly simple, but my brain is frozen right now. Need a bit of assistance with this query. Let's break it down.
I have two tables (per this example) and I want to update a single table "undeliverable" status
Customers Table (tbl_customers):
+------------+-------------+
| customerID | custAcctNum |
+------------+-------------+
| 1 | 100100121 |
| 2 | 100100122 |
| 3 | 100100123 |
| 4 | 100100124 |
| 5 | 100100125 |
+------------+-------------+
Address Table (tbl_address):
+-----------+------------+---------------+
| addressID | customerID | undeliverable |
+-----------+------------+---------------+
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 3 | 0 |
| 4 | 4 | 0 |
| 5 | 5 | 0 |
+-----------+------------+---------------+
Dataset with "undeliverable" Customer Account numbers (custAcctNum)
100100121, 100100123, 100100124
And the query will update the Address Table to this
+-----------+------------+---------------+
| addressID | customerID | undeliverable |
+-----------+------------+---------------+
| 1 | 1 | 1 |
| 2 | 2 | 0 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 5 | 0 |
+-----------+------------+---------------+
This is the query that I have tried to use
UPDATE tbl_address
SET undeliverable = 1 WHERE
( SELECT custAcctNum FROM tbl_customers AS c
INNER JOIN tbl_address AS a ON a.customerID = c.customerID )
IN ( 100100121, 100100123, 100100124);
Any suggestions? Thanks!
Use mysql's multiple-table update syntax:
update tbl_Address t
join custAcctNum c
on c.customerid = t.customerid
set t.undeliverable = 1
where c.custAcctNum in (100100121, 100100123, 100100124)
UPDATE tbl_address
SET (undeliverable = 1)
WHERE customerID IN (
SELECT customerID
FROM tbl_customers
WHERE custAcctNum IN (100100121, 100100123, 100100124)
);