How to get Data form a table based on the foreign key in sql - sql

Table 1
Table 2
Here wish to get the data from Table 1 which is created between 5\09\2018 to 26\12\2018.
Here PostID is the foreign key.
Thanks in advance

You can join both tables and use where statement to retrieve the relevant data,
Select *
from table1 t1
inner join table2 t2
on t1.PostID=t2.PostID
where t1.datecreated between '2018-09-05' and '2018-12-26'

Select *
from table1 t1
inner join table2 t2
on t1.PostID=t2.PostID
where t2.PostID=1
For inner join foreign key is mandatory, and the reference column will come in the join condition.

If you just query data from Table 1, you may write the query like this.
select
*
from table1 T
where T.DateCreated between '2018-09-05 00:00:00' and '2018-12-26 23:59:59'
and exists (select 1 from table2 where PostID=T.PostID)
Best Regards,
Will

Related

Sql query with join on table with ID not match

I have two tables.
Table 1
Id
UpdateId
Name
Table 2
Table1ID
UpdateID
Address
Each time user update, system will insert record to table1. But for table2, system only insert record when there is update in address.
Sample data
Table 1
1,1,name1
1,2,name1
1,3,name1update
1,4,name1update
1,5,name1
1,6,name2
Table 2
1,1,address
1,4,addressupdate
I want to get the result as following
1,1,name1,address
1,2,name1,address
1,3,name1update,address
1,4,name1update,addressupdate
1,5,name1,addressupdate
1,6,name2,addressupdate
How to make use of join condition to achieve as above?
You can use a correlated subquery. Here is standard syntax, but it can be easily adapted to any database:
select t1.*,
(select t2.addressid
from table2 t2
where t2.table1id = t1.id and
t2.updateid <= t1.updateid
order by t2.updateid desc
fetch first 1 row only
) as addressid
from table1 t1;
you can use left join when you want to take all columns from left table t1 even though it doesn't match with the other table with column updateid on t2 table.
select t1.id,t1.updateid,t1.name,t2.address from table1 t1
left join table2 t2
on t2.updateid= t1.updateid
you can read more about joins here

INNER JOIN with a large table

I am currently using Microsoft Access 2013, and I am trying to join Table1 and Table2 together, but the problem is that Table2 is massive. Table1 is a list of part, vendor combinations with PK as part, vendor. Table2 is a table I created with the top2 most recent quotes for each part,vendor combination. All these quotes were pulled from a table with PK quote_id. I think my creation of Table2 might be the problem, because I cannot create Table2 with every part,vendor combination (i have to filter out by vendor). This is the query I used for Table2.
a.part, a.vendor, a.quote_date
FROM quoteTable AS a
WHERE a.quote_date > DATEADD("yyyy", -3, DATE()) AND
a.quote_date IN
(SELECT TOP 2 quote_date
FROM quoteTable
WHERE quote_date > DATEADD("yyyy", -3, DATE()) AND
part=a.part AND vendor=a.vendor
ORDER BY quote_date DESC)
If anyone knows a better way to select the top 2 most recent quotes from the table for each part,vendor combination, I would really appreciate it. As for the join, this works but would take too long.
SELECT *
FROM Table1 AS a INNER JOIN Table2 AS b ON a.id = b.id
I am wondering if there was a way I could use the id from Table1 to filter Table2? Something like this:
SELECT *
FROM Table1 AS a INNER JOIN
(SELECT * FROM Table2 WHERE id=a.id) AS b ON a.id = b.id
You definitely could use:
SELECT *
FROM Table1 AS a
INNER JOIN
(SELECT * FROM Table2 WHERE id=a.id) AS b
ON a.id = b.id
Or you could use :
With CTE
as
(
SELECT *
FROM Table2
WHERE id in (select id from table1)
)
SELECT *
FROM Table1 AS a
INNER JOIN
CTE
ON CTE.Id = a.Id
For performance issue, you could try to create index on id column from both tables, or try to limit the selected column from your result.
The real solution is that you need an index on TABLE2.id. I am assuming that it is not the primary key of that table, because it probably is the primary key of TABLE1, and why would you have two tables with the exact same, matching primary keys? It would not be a normalized layout. But then again, there are times when it makes sense to de-normalize.

Join tables with Two foreign keys

I am searching for a real scenario problem that I faced last night while joining two tables with foreign keys. Actually I want to get all values from second table on behalf of foreign key.
Here are my two tables let suppose:
table1 (id_user_history(PK),id_user(FK), order_no, p_quantity)
table2 (id_shoping_cart(PK), id_user(FK),order_id, prod_quantity)
Now I want to get all values from table2 by joining these tables with table1(id_user(Fk)) and table2( id_user(FK))
SELECT *
FROM table2 t2
LEFT JOIN
table1 t1
on t1.id_user = t2.id_user
all records from table 2 and only those record which match on table 1.
SQL is mainly set logic. Here's a link which helps visualize.
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Looks like a simple join fits the bill:
select *
from table1 t1
left join
table2 t2
on t1.id_user = t2.id_user

sql make the joined tables as one table may be?

Is there any way to name the complete select as a table? I will try to explain what I am trying to do. I have this
SELECT
*
FROM
`Databse1`.`table1`
JOIN
`Database2`.`table2`
ON
`table2`.`customerID` = `table1`.`customerID`
WHERE
`table1`.`recordID` IN (1,2,3,4)
I have another table, table3 that has these fields
customerID
recordID
the recordID is foreign key to table1.
What I want to do is in above query somehow enter customerID so
it can get all the recordIDs. Is that possible?
Sounds like you want a derived table
SELECT
*
FROM Table3 t3
JOIN (SELECT
*
FROM `Databse1`.`table1`
JOIN `Database2`.`table2` ON `table2`.`customerID` = `table1`.`customerID`
WHERE
`table1`.`recordID` IN (1,2,3,4)) t1 ON t1.customerID = t3.customerID
WHERE t3.customerID = [your customer id]

What is the standard SQL Query to retrieve the intersection of tables?

Selecting the union:
select * from table1
union
select * from table1_backup
What is the query to select the intersection?
In SQL Server intersect
select * from table1
intersect
select * from table1_backup
SELECT *
FROM table1
WHERE EXISTS
(SELECT *
FROM table1_backup
WHERE table1.pk = table1_backup.pk)
works
For questions like this, I tend to go back to this visual resource:
A Visual Explanation of SQL Joins
inner join i think:
suppose T1 and T2 have the same structure:
select T1.* from
T1 inner join T2 on T1.pkField = T2.pkField
"intersect" is also part of standard SQL.
Inner join gives a different answer.
here is a solution for mySQL:
CREATE TABLE table1(
id INT(10),
fk_id INT(10),
PRIMARY KEY (id, fk_id),
FOREIGN KEY table1(id) REFERENCES another_table(id),
FOREIGN KEY table1(fk_id) REFERENCES other_table(id)
);
SELECT table1.* FROM table1 as t0
INNER JOIN table1 as a ON (t0.id = a.id and fk_id=1)
INNER JOIN table1 as b ON (t0.id = b.id and fk_id=2)
INNER JOIN table1 as c ON (t0.id = c.id and fk_id=3)
ORDER BY table1.id;
Basically you have an table of mathematical subsets (ie. 1={1, 2 ,3}, 2={3, 4, 2}, ... , n={1, 4, 7}) with an attribute id, which is the set number, and fk_ id, which references a PRIMARY KEY of a table of elements, the superset (meaning possible values for the numbers in the curly braces). For those not mathematically inclined, let's pretend you have a table, 'other_ table', which is a list of items, and another table, 'another_ table', which is a list of transaction numbers, and both tables form a many-to-many relationship, thus producing 'table1'. now let's pretend you wanted to know the id's in 'another_ table' which had items 1, 2, and 3. that's the query to do it.
An intersect on two identical tables a and b can be done in this manner:
SELECT a.id, a.name
FROM a INNER JOIN b
USING (id, name)
subqueries?! really?
to get the intersection of table1 and table2:
SELECT * FROM table1, table2 WHERE table1.pk=table2.pk;
select distinct * from (select * from table1 union select * from table1_backup)