Inner join returns same columns from two tables access sql - sql

I have 2 tables and i'm inner join them using EID
CSCcode Description BNO BNO-CSCcode E_ID
05078 blah1 5430 5430-05078 1098
05026 blah2 5431 5431-05026 1077
05026 blah3 5431 5431-05026 3011
04020 blah4 8580 8580-04020 3000
07620 blah5 7560 7560-07620 7890
07620 blah6 7560 7560-07620 8560
05020 blah1 5560 5560-04020 1056
Second table
y/n EID
y 1056
n 1098
y 1077
n 3011
y 3000
n 7890
n 8560
I'm selecting all fields from table one and y/n field from table 2. but it retrieve all from table 2 including EID. I don't want to retrieve EID from table2 because result table will have two EID fields.
My query
SELECT *, table2 .EID
FROM table1 INNER JOIN table2 ON table1 .E_ID = table2 .EID;

"I'm selecting all fields from table one"
No, you are selecting all fields from all tables. You need to specify the table if you only want all fields from one table:
SELECT table1.*, table2.EID
However, using * is not good practive. It's better to specify the fields that you want, so that any field that you add to the table isn't automatically included, as that might break your queries.

Your query is selecting everything from both tables as well as specifically the EID column from table 2. Change it to:
SELECT table1.*, table2.y/n
...
Although, as stated in other answers, avoid using * and instead list the column names table.columnName, etc.

Specify the table from which you want to select all fields modify your query to
SELECT
table1.*, table2.EID
FROM
table1
INNER JOIN table2
ON table1.E_ID = table2.EID;
If you are using the query designer, Access sometimes sets the property Output All Fields of the query to Yes. Make sure it is set to No and select the fields you want manually. (Right click in the designer and select Properties... in the context menu in order to see the properties.)

The reason you sometimes get stuck with a surprise * is because of a property in the query designer. Change this to No in the designer. Alternatively, like the other answers say, just remove the * from your query.

You can't do things like this 'SELECT *, table2 .EID' - you should include all your fields from table 1. However, it is not a good practice even if you are selecting from one table.
SELECT
table1.CSCcode,
table1.Descriptio,
table1.BNO,
table1.BNO-CSCcode,
table1.E_ID,
table2 .EID
FROM
table1 INNER JOIN table2 ON table1 .E_ID = table2 .EID

Related

SQL Server : using another table as a 'lookup'

I am selecting some data from a table where one of the columns is a value that defines a person. The value is unique to that person but can appear multiple times in the table.
I have no documentation whatsoever on the db - the details of the person (name etc) are held in another table, but that has a different ID column and does not reference the ID in the first table.
There is a third table (stay with me!) that contains both the ID from the first table, and the id from the second table. So I'm trying to use this to create a 'link' between the two IDs, so I can then grab the person data I need from the second table. The third table contains many more records than the other two.
I've tried various joins but the data I get back always contains duplicates, and incorrect IDs. I'll try and give an example:
Table 1 (the table I'm querying):
AgentID
Date
Time
9000
1/1/2022
12:00:00
9000
1/2/2022
15:00:00
9001
1/1/2022
13:00:00
9001
1/2/2022
17:00:00
Table 2 (the user info):
UserID
Name
1000
Fred Bloggs
1001
John Smith
Table 3 (the 'link'):
UserID
AgentID
Other Data
1000
9000
…
1001
9001
1001
9001
1000
9000
1001
9001
1000
9000
etc
Neither UserID or AgentID are PKs or FKs in the respective tables (don't ask - we didn't design this....)
Is there a way to do what I'm trying to do? It seems there should be but I've hit a brick wall.
Example of what I've tried:
SELECT
table2.name, table1.date,table1.time
FROM
table2
LEFT JOIN
table3 ON table3.agentID = table1.agentID
LEFT JOIN
table2 ON table2.UserID = table3.UserID
I've tried inner and right joins as well, same results.
Do I need to be doing some sort of 'nested' join?
you should use Subquery, first you should join table1 with distinct value of table3(Link), second you should join table2 (UserInfo)with table link. following query
SELECT U.Name,
T.Date,
T.Time
FROM table1 t
JOIN (SELECT DISTINCT UserID,
AgentID
FROM table3) L
ON T.AgentID= L.AgentID
JOIN table2 U
ON L.UserID= U.UserID
you should change the query per the names of your tables.

Get the list for Super and sub types

Am Having the Tables in SQL server as Super and Sub types like below. Now if i have to get list of Furnitures then how can i get the list?
Furniture table:
Id FurnituretypeId NoofLegs
-------------------------------
1 1 4
2 2 4
FurnitureType table:
Id Name
-----------------
1 chair
2 cot
3 table
Chair Table:
Id Name CansSwing CanDetachable FurnitureId
------------------------------------------------------------
1 Chair1 Y Y 1
Cot Table:
Id Name CotType Storage StorageType FurnitureId
-------------------------------------------------------------------
1 Cot1 Auto Y Drawer 2
How can i get the entire furniture list as some of them are chair and some of them are cot. How can i join the these tables with furniture table and get the list?
Hmmm . . . union all and join?
select cc.*, f.*
from ((select Id, Name, CansSwing, CanDetachable,
NULL as CotType, NULL as Storage, NULL as StorageType, FurnitureId
from chairs
) union all
(select Id, Name, NULL as CansSwing, NULL as CanDetachable,
CotType, Storage, StorageType, FurnitureId
from cots
)
) cc join
furniture f
on cc.furnitureid = f.id;
This is a classical learning problem, that's why I'm not giving you the code to solve this but all the insights you need to do so.
You have multiple approaches possible, but I'm describing two simple ones:
1) Use the UNION statement to join two separate queries one for Chair and the other for Cot, bare in mind that both SELECT have to return the same structure.
SELECT
a1,
a2,
etc..
FROM table1 a1
JOIN table2 a2 ON a1.some = a2.some
UNION
SELECT
a1,
a3,
etc..
FROM table1 a1
JOIN table3 a3 ON a1.some = a3.some
2) You can do it all in one SELECT statement using a LEFT JOIN to both tables and and in the select using COALESCE or ISNULL to get the values for one table or the other. In the WHERE condition you have to force one or the other join to be not null.
SELECT
a1,
COALESCE(a2,a3) as col2
FROM table1
LEFT JOIN table2 a2 ON a1.some = a2.some
LEFT JOIN table3 a3 ON a1.some = a3.some
WHERE
a2.some IS NOT NULL
OR a3.some IS NOT NULL
Mapping objects into relational models takes a degree of understanding of what is possible vs. what is wise in an RDBMS. Object oriented database systems tried to go after problems like this (generally without much success) precisely because the problem statement is arguably not the right one.
Please consider just putting all of these in one table. Then use null for the fields that don't really matter for each sub-type. You will likely end up being a lot happier in the end since you can spend less time at runtime doing joins and instead just query the information you need and use indexing on the same table to find the fasted path for each query you want to run.
SELECT * FROM CombinedTable;

How to concatenate two tables with matched columns into a single view

I have two tables in a Microsoft Access 2007 database.
One of them has columns "name 1 & name 2" with 2000 records. The other table has more information: it has two columns named "code1 & code2" with 3000 records. Each name has a code means that every name has a code stored in the second table in the column code 1.
I want to make one table or query that shows me name 1 & name 2 with their specific code1 & code 2 from the second table .
And it must have only 2000 records
example :
tabel 1 :
name-1 name-2
-----------------------------
Abacavir Digoxin
Amprenavir Aspirin
tabel 2 :
code-1 drug1
----------------
xy1 Abacavir
xy2 Digoxin
yxr1 Amprenavir
uyv2 Aspirin
sample output :
name-1 code-1 name-2 code-2
-----------------------------------------
Abacavir xy1 Digoxin xy2
This table structure is poorly designed but I'll assume you are stuck with it. You have to create a query that joins Table2 twice... once for each of the relationships you are linking by. In my test I created an access db and named my tables simple Table1 and Table2 so you may need to alter this query slightly to match your table/column names.
SELECT Table1.name1, Table2a.code1, Table1.name2, Table2b.code1
FROM (Table1 LEFT JOIN Table2 AS Table2b ON Table1.name2 = Table2b.drug1) LEFT JOIN Table2 AS Table2a ON Table1.name1 = Table2a.drug1;
The fact you have 2K in the first and 3K in the second is a clue. Try adding the 'Distinct' key in your SQL statement:
SELECT DISTINCT Table1.name1, Table2a.code1, Table1.name2, Table2b.code1
FROM (Table1 LEFT JOIN Table2 AS Table2b ON Table1.name2 = Table2b.drug1) LEFT JOIN Table2 AS Table2a ON Table1.name1 = Table2a.drug1;
Otherwise I think Frank nailed it.

Update a single table based on data from multiple tables SQL Server 2005,2008

I need to update table one using data from table two. Table one and two are not related by any common column(s). Table three is related to table two.
Ex : table one(reg_det table)
reg_det_id | reg_id | results
101 | 11 | 344
table two :(temp table)
venue | results
Anheim convention center | 355
Table three (regmaster-tbl)
reg_id| venue
11 | Anaheim convention center
I need to update results column in table one using data from table two. But table one and two are not related. Table two and three and table one and three are related as you can see above. Can anyone please suggest any ideas! I need the results value to be 355 in table one and this data is coming from table 2, but these two are unrelated, and they can be related using table three. Sorry if it is confusing!
Fairly straight forward:
UPDATE T1
SET result = t2.results
FROM [table one] T1
INNER JOIN [table three] t3
on t1.reg_id = t3.reg_id
INNER JOIN [table two] T2
on t2.venue = t3.venue
Almost a question instead of an answer. :)
Couldn't you use an implied inner join?
UPDATE rd
SET rd.results = tt.results
FROM reg_det rd, regmaster rm, temptable tt
WHERE rm.reg_id = rd.reg_id
AND rm.venue = tt.venue;
I find it easier to read, and this syntax works in a SELECT statement, with the same meaning as an explicit inner join.
Try this:
UPDATE rd
SET rd.results = t.results
FROM reg_det rd
JOIN regmaster rm ON rm.reg_id = rd.reg_id
JOIN temptable t ON t.venue = rm.venue
WHERE t.results = 355
I added a WHERE clause because otherwise it will update all reg_det records that have matches in regmaster and temptable.

Simple SQL Select from 2 Tables (What is a Join?)

I'm new to SQL. I have a simple problem with getting the results from two different tables.
I have two tables in a database. The first table has a column with an id reference, which corresponds to rows in the second table. What SELECT do I need to perform to get a result such that the ids are repalced by all of the values in the second table. To visualize the tables I am discussing:
TABLE_USERS
===========
id username group
-- -------- -----
1 jim A
2 alice A
3 brandon B
TABLE_GROUPS
============
id groupname members
-- --------- -------
A designer 134
B photographer 39
DESIRED_SELECTION
=================
id username group
-- -------- -----
1 jim designer
2 alice designer
3 brandon photographer
Thanks!
You do, in fact, want to JOIN the two tables:
SELECT * FROM
TABLE_USERS LEFT JOIN TABLE_GROUPS
ON TABLE_USERS.group = TABLE_GROUPS.id
The trick of joining tables is to find the values that must match in the two tables, and use the on to tell SQL to match them. This table has a ID column to let you do that = you will join the table, ON, and then list the values that need to be equal.
If you do not want all of the columns in both tables, you can simply list only the columns you need in your final query. This means that instead of Select *, you list the columns you want. As shown below, if a column appears with the same name in both tables, you need to prepend the table name, so that SQL know which value you want.
SELECT TABLE_USERS.ID, Username, Groupname
FROM TABLE_USERS
LEFT JOIN TABLE_GROUPS
ON TABLE_USERS.group = TABLE_GROUPS.id
You want a JOIN:
SELECT
u.id,
username,
groupname
FROM
TABLE_USERS AS u
LEFT JOIN TABLE_GROUPS AS g
ON u.group = g.id