As stated in the title, I'm getting duplicate columns with this JOIN query.
A few tables are given and I want to write select statements to get only
the information from the tables which are needed.
Here is my SQL code so far:
SELECT mitarbeiter.PNR, pfleger.PNR, Name
from pfleger
JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR)
where Ort='Frankfurt';
After executing, I get the following result:
You can see the problem: I have two PNR columns which I don't want to have.
How can I remove the duplicate? I have tried SELECT DISTINCT ... but it doesn't accomplish my goal.
Just remove one from the select:
SELECT mitarbeiter.PNR, Name from pfleger JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR) where Ort='Frankfurt';
select distinct applies to rows, not columns. In the column list, just select one of the PNR columns:
SELECT mitarbeiter.PNR, Name from pfleger JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR) where Ort='Frankfurt';
In the select portion of the statement reference the PNR column from either of the tables (mitarbeiter, pfleger), but not both:
SELECT
mitarbeiter.PNR,
Name
from pfleger JOIN mitarbeiter on (mitarbeiter.PNR=pfleger.PNR)
where Ort='Frankfurt';
As other users have already mentioned, you just need to remove a field name from the SELECT clause. I just want to add that if the field you join on has the same name in both tables you can use special syntax, which allows to reference both columns as a single one:
SELECT PNR, Name
from pfleger
JOIN mitarbeiter USING (PNR)
where Ort='Frankfurt';
Related
I have sql query like this
SELECT * FROM phlegm WHERE JOIN mucus ON phlegm.id = mucus.id JOIN snot ON phlegm.id = snot.id
The problem is those tables contain several columns with identical names.
For example all 3 tables contain the column named test
If I retrieve the result of the query in PHP, then I will only get one value named test ($query->get_result()->fetch_object()->test;), because the other two get overwritten.
Is there some way to edit that query so that it adds a prefix to all columns from a table? For example, column test from table mucus would be referenced in the query as mucus_test and column test from phlegm would be phlegm_test.
One way would be doing
SELECT phlegm.test as phlegm_test, mucus.test as mucus_test FROM phlegm...
But I have a LOT of columns and tables and it would make the query longer than the Great Wall of China if I had to name each field one by one.
So is there some way to add the prefix en masse?
SELECT *, phlegm.test as phlegm_test, mucus.test as mucus_test FROM phlegm...
Used aliasing to retrieve all values associated from all three tables. if you want to reference only specific column do so by using the alias_name.column_name instead of p.*, where * means all columns belonging to table that the alias is associated with( ie. p refers to phlegm).
SELECT p.*, m.*, s.*
FROM phlegm p
JOIN mucus m ON p.id = m.id
JOIN snot s ON p.id = s.id;
I removed the WHERE from your original query above, not sure why it was there.
I have a question about merging a table with another preserving an ID on a database (I'm using MySQL). I have 2 tables, the first has and Item ID and a category and subcategory assigned to that ID. The second has a Item ID with all its characteristics like name and other variables. How can I merge those two tables in a way that the ID corresponds to the correct item in the new table (that's the difficult part I think)? Is it possible?
Thank you for all the help!
It's a very basic operation called Inner Join:
Select *
from table1
inner join table2
on table1.itemid = table2.itemid;
EDIT: As OP wants to create a new table with the fields return by above query and insert data into newly created table; following are the query to insert data once its created:
Insert into tablename
Select *
from table1
natural join table2;
Note: Make sure that the order and datatypes of columns in new table and in the result of above select query must be same.
I'm assuming you want to create table from the combined results. See this page for details.
Basically you write and test the SQL query then CREATE TABLE table_name AS sql_query
create table new_item_table
as
select
a.item_id,
a.category,
a.subcategory,
b.item_name,
b.item_char_1,
b.item_char_2
from
item_category a inner join item_char b on a.item_id = b.item_id;
This will Do:
select a.*,b.ItemName,b.ItemChar1,b.ItemChar2 from FirstTable a join select * from SecondTable b on a.ItemId=B.ItemId;
Use left join if some of the records are not there in the second table
I need to join 3 tables which have some columns with same name, like id and some foreign keys columns.
I make a select query and the results come with table names only. How to get results like "dbname"."columnname" in my queries so I can identify from which table is each columns without having to specify every columns in the query (using only an *)?
Note: I use Delphi with ZeosLib, so a solution using these tools would be OK as well. But I prefer to set this in the data base.
You have to create an alias for your field name in your query
SELECT a.ID, b.ID
FROM a
JOIN b
You need doblue quote " for field names with special characters, so change it to.
SELECT a.ID "a.ID", b.ID "b.ID"
OR
SELECT a.ID "MeaningfullName", b.ID "OtherName"
For example here I have two fields name "sent_dt" and change one to previous_time
SQL Fiddle Demo
i have a problem in my query to select from two table in a case like that
savanh table that hold student data and add_class table that hold foreign key of savanh that is savanh_id, now i want to select just those student form savanh table that not added in add_class table yet.
below query not work please any idea?
SELECT savanh.savanh_id,asass_number, name, father_name, grand_father
FROM savanh, add_class
WHERE savanh.savanh_id != add_class.savanh_id;
YOu can for example use a LEFT JOIN:
A left join will give you the data in savanh as base data. Then the data of add_class is matched against that.
All records which can not be matched, are the ones we want to keep, which gives you your WHERE clause with the NULL check.
SELECT savanh.savanh_id,asass_number, name, father_name, grand_father
FROM savanh
LEFT JOIN add_class ON savanh.savanh_id = add_class.savanh_id
WHERE add_class.savanh_id IS NULL;
I am wanting to select 1 column from my select statement as but then leave the rest as is so:
SELECT tbl_user.reference AS "reference", * FROM tbl_user JOIN tbl_details ON.....
Is this possible?
Yes. You can use double quotes like that to create a column alias. You can SELECT a column twice (or more) in your SELECT list.
Try something like this, where you can give each "reference" column its own alias:
SELECT u.reference AS UserReference,
d.reference as DetailsReference,
u.id, /*etc etc*/
FROM tbl_user AS U
JOIN tblDetails AS D ON ....
You mention in the comments that you want all columns from each table, while being able to distinguish between the reference columns(likely named the same in both tables). Suggest NOT using SELECT *, as it's an anti-pattern. It's most beneficial to specify your column list in your SELECT statement, and do your query engine a favour of not having to look up the list of columns on each table.
If you just want one column, this will work:
SELECT SELECT tbl_user.username AS "username" FROM tbl_user JOIN tbl_details on tbl_user.key LIKE tbl_details.key
What do you mean by "but then leave the rest as is "?