sql make the joined tables as one table may be? - sql

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]

Related

How to get Data form a table based on the foreign key in 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

Appending and Mapping of two tables in SQL

I have two tables in the database, I want to merge and map the table 2
into table 1
by writing SQL query.
I don't know whether to apply joins or what? and then how to append the data.
note: Customer id is the common key in both tables.
A simple INNER JOIN would solve your problem --
SELECT T1.[Branch Code]
,T1.[CustomerID]
,T2.[Customer Name]
,T1.[Advised Amount]
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.CustomerID = T2.CustomerID
Use Inner join..... U can write column name in place of *
Select * from Table1 as a
inner join Table2 as b on a.CustomerId = b.CustomerId

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.

SQL - How to select master records that have no detail records?

I have two tables (Table1 - master table, Table2 - detail table). How can I select (view) only those records from master table that have no records in detail table? What SQL statement can I use for this? I'm using MS SQL Server 2012.
Edit: Table definition
Table1 - ID (PK)
Table2 - ID (PK), Table1ID (FK)
Use NOT IN Operator
SELECT *
FROM Table1
WHERE ID NOT IN(SELECT Table1ID
FROM Table2 )
I would use NOT EXISTS since it's clear, efficient and has no issues with nullable columns.
For example (MasterID is the PK/FK):
SELECT master.*
FROM dbo.Table1 master
WHERE NOT EXISTS
(
SELECT 1 FROM Table2 detail
WHERE detail.MasterID = master.MasterID
)
But you have other options: http://sqlperformance.com/2012/12/t-sql-queries/left-anti-semi-join
How about using an exclusive (left) outer join?
SELECT
master.*
FROM
master LEFT OUTER JOIN details
ON master.ID = details.masterID
WHERE details.ID IS NULL;
Have a look here for a more detailed description why this query is a solution to this kind of problem.
I prefer using left joins in such cases:
select tp.*
from table_parent tp
left join table_child tc on tc.parent_id = tp.id
where tc.parent_id is null
this will help you greatly in answering your question:
http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html
SELECT column1 FROM t1 WHERE NOT EXISTS (SELECT * FROM t2);

Query to get name from a table with 2 ID columns

I have 2 tables. The first has a structure like
id (int)
parent_id (int)
category_name (varchar)
The second has a structure like:
id (int)
old_category (int)
new_category (int)
I want to make a query to pull the old category and new category names all in one query for the parents. That is, I want to get the name of the old parent, then get the name of the new parent. The second table contains a historical list of all the parent ID changes made in the first table. How can I do this? Thanks!
SELECT old_cat.category_name, new_cat.category_name
FROM join_table
LEFT JOIN category_table as old_cat
ON old_cat.id = join_table.old_category
LEFT JOIN category_table as new_cat
ON new_cat.id = join_table.new_category;
If you've managed to get this far, it's time to stop coding and start learning MySQL JOIN Syntax.
SELECT
t.id,
t.old_category,
oc.category_name as old_category_name,
t.new_catevory,
nc.category_name as new_category_name
FROM
table1 t
INNER JOIN table2 oc on oc.id = t.old_category
INNER JOIN table2 nc on nc.id = t.new_category
I would seriously consider some modifications in column names, btw. To start: why don't the categories in table2 have an id postfix?
I will take a guess at what you meant:
tblOne
id
cat_name
tblTwo
id
tblOneId (old_cat) int
new_cat int
select * from tblOne t1, tblTwo t2 Where t1.id = t2.tblOneId
Join them?
select * from table1
join table2 j1 on table2.id = table1.old_category
join table2 j2 on table2.id = table1.new_category