SQL query to find unreferenced records - mysql

I have two tables bound through an ID field:
table1: id, name, type
table2: id, id_table1, date, status
I have to collect all the records of the table1 that have a certain value of type field and that are not been referenced in table2 plus all the records of table1 referenced in table2 that have a certain status field value.
For the first part if I remember correctly I can use the LEFT JOIN command:
LEFT JOIN table1.name
LEFT JOIN table2
ON table2.id_table1 = table1.id
WHERE (table1.value = 'value1') AND (table2.id_table1 IS NULL);
but for the second part I'm getting lost...
I'm using MySQL 5.6 and I would like to define a View to handle this.

SELECT t1.*, t2.*
FROM table1 t1
LEFT JOIN table2 t2
ON table2.id_table1 = table1.id
WHERE (t1.type= 'value1' AND t2.id IS NULL)
OR (t2.status = 'certain status' )

I would think you could just change the WHERE to:
WHERE (table1.value = 'value1')
AND (table2.id_table1 IS NULL
OR
([the other table2 status criteria)
)
;

You can try this...
SELECT T1.*,T2.*
FROM Table1 T1
LEFT JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T1.Value = 'value1' AND T2.id_table1 IS NULL
UNION
SELECT T1.*,T2.*
FROM Table1 T1
INNER JOIN Table2 T2 ON T1.Id=T2.Id_Table1
WHERE T2.Status= 'Status Criteria'

Related

how to return all the fields of table2 based upon the occurrence of the id in the table1

I have 2 tables, one is table1
and another is table 2
I want the result by a query, like
I have tried select id from table2 order by (select id from table1); but it is giving error.
You can join and sort. But you need a column that defines the ordering of the rows in table1. Let me assume that you have such column, and that is is called ordering_id.
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id
order by t1.ordering_id
You can even use a subquery in the order by clause:
select *
from table2 t2
order by (select t1.ordering_id from table1 t1 where t1.id = t2.id)
Join the two tables and then order the result.But for that you need to have some column for ordering and this does not seems to be the case. Syntax you are using for ordering will not work.
SELECT A.ID, B.NAME FROM TABLE1 A INNER JOIN TABLE2 B
ON(A.ID = B.ID) ORDER BY A.ID DESC
finally got the answer
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id;

One SQL statement for two Foreign Keys

I have two tables. The first table contains ID, First_Name and Last_Name.
The 2nd table contains two foreign key fields containing different ID's of the first table.
I want to be able to run a SQL query that gets reults of the 2nd table which then grabs the First_Name of each member based on the two different foreign keys.
How would I go about doing this?
select t2.*, t1a.firstname, t1b.firstname
from table2 t2
left join table1 t1a on t2.fk1 = t1a.id
left join table1 t1b on t2.fk2 = t1b.id
Suppose the second table has fields as such
userid, supervisorid ( both referring to the Id column of the first table )
you may write join to get the value like this
SELECT t2.*, ID, firstname, lastname FROM table 2 t2
LEFT OUTER JOIN table 1 t1 ON
t2.userid = t1.id
OR t2.supervisorid = t1.id
I think correct sql would be below one using OR condition in outer join or using union
SELECT t1.id,t1.name from table1 t1, table2 t2 WHERE t1.id1 = t2.id1
UNION
SELECT t1.id,t1.name from table1 t1, table2 t2 WHERE t1.id1 = t2.id0
SELECT t1.id, t1.name from table2 t2 LEFT OUTER JOIN table1 t2 ON t1.id = t2.id or t1.id1 = t2.id0

MYSQL: How to make it virtual column in select Query?

Below is the my question any one have idea how to resolve this issue:
Example Query:
SELECT id, table2.id as global_id, table3.id as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc
Basically i don't want to write two times "global_id" column in the select statement and also in the some rows have id = NULL
Can you please write working query here. How can i get id value of all rows in the global_id column ?
Thanks
Use COALESCE:
SELECT table1.id, COALESCE(table2.id, table3.id) as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc
Guess you need this:
SELECT id, table2.id as global_id
FROM table1
LEFT JOIN table2
ON ( table1.id = table2.tab1_id )
WHERE etc etc
UNION ALL
SELECT id, table3.id as global_id
FROM table1
LEFT JOIN table3
ON ( table1.id = table3.tab1_id )
WHERE etc etc

SQL Server - exclude data where there's no connection to second table

The example below shows the result for every Name that has a connection to Table2 (Table1 TId is PK, and TId in Table2 is the FK).
SELECT T1.Name, T1.Address
FROM Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.TId = T2.TId;
I want a list of all Names from Table1 that have NO corresponding row in Table2. The other way around so to speak. How could this be done?
You need to use an Outer Join as shown below:
SELECT T1.Name, T1.Address
FROM Table1 AS T1
LEFT OUTER JOIN Table2 AS T2 ON T1.TId = T2.TId
WHERE T2.TId IS NULL

Mysql join on similar columns

I want to join table1 with table2 on column 'Name', but table2.Name has an 'e' in front of all the names (if table1.name=ABC,table2.name=eABC). How am I supposed to use a join for those two?
I tried FROM table1 join table2 on 'e'+table1.name = table2.name, but it doesn't work...
SELECT *
FROM table1 t1
JOIN table2 t2
ON t2.name = CONCAT('e', t1.name)
Try using a substring of the table2 name. So something like:
SELECT *
FROM table1
, table2
WHERE table1.name = substring(table2.name, 1, length(table2.name))
I can't remember if substring is zero based, so just play with the numbers.