I have 2 tables connected via join. Both tables share some column names. I need to distinguish between those columns. So I am looking for something like this:
SELECT t1.* AS t1_* , t2.* AS t2_*
FROM t1
LEFT JOIN t2
ON t1.id=t2.somefield
Meaning if in both table is a column "place", the column names should appear in the result list as "t1_place" and "t2_place".
I did not find if this would be possible somehow. The reason is that both tables are rather large so I try to avoid listing all the columns and assigning aliases.
Any suggestions?
Related
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
So my problem is as follow, I have a table in MySQL with a UserId column and an ObjectId column (its a many to many relationship), and what I would like is to have is a query that gives me the list of objects that user X and Y share. Not sure how to make the joins to make this happen.
Use query something like below using self join
Select columns from table t1 join table t2 on t1.objectid=t2.objectid where t1.userid=X and t2.userid=Y
I have an issue with some of the join statements I'm trying to use. I have two tables that need to be joined, with both featuring all of their information. They're as follows.
INSTITUTION
IName | ALocation_ID | IPicture
ADDRESS
ALocation_ID | AStreet | AZip | ...(other relevant fields)
I've been trying to use:
CREATE VIEW InstitutionView
AS SELECT * FROM INSTITUTION
LEFT JOIN ADDRESS
ON INSTITUTION.ALocation_ID=ADDRESS.ALocation_ID;
but the error I receive says something about duplicate columns. What am I doing wrong?
You will have to select the columns individually. Hopefully this helps you out a little.
CREATE VIEW InstitutionView
AS
SELECT address.id,address.iname,address.alocation_id,ipicture,institution.astreet,institution.azip
FROM INSTITUTION
LEFT JOIN ADDRESS
ON INSTITUTION.ALocation_ID=ADDRESS.ALocation_ID;
That is because ALocation_ID column is present in both the tables.
Try creating the view explicitly naming the required columns.
CREATE VIEW InstitutionView
AS SELECT Iname,INSTITUTION.ALocation_ID,IPicture,AStreet,AZip ...
FROM INSTITUTION
LEFT JOIN ADDRESS
ON INSTITUTION.ALocation_ID=ADDRESS.ALocation_ID;
Conceptually, JOIN first creates an intermediate cross-product where the columns are referenced by a table name or alias dotted by a column name from that table; then ON and WHERE filter out rows that don't match to give a second intermediate result. If a column name appears only in one table then you can leave out the table & dot to refer to the column.
MySQL 5.6 Reference Manual :: 13.2.9 SELECT Syntax
You can refer to a column as col_name, tbl_name.col_name, or db_name.tbl_name.col_name. You need not specify a tbl_name or db_name.tbl_name prefix for a column reference unless the reference would be ambiguous. See Section 9.2.1, “Identifier Qualifiers”, for examples of ambiguity that require the more explicit column reference forms.
MySQL 5.6 Reference Manual :: 9.2.1 Identifier Qualifiers
Suppose that tables t1 and t2 each contain a column c, and you retrieve c in a SELECT statement that uses both t1 and t2. In this case, c is ambiguous because it is not unique among the tables used in the statement. You must qualify it with a table name as t1.c or t2.c to indicate which table you mean.
Hence:
CREATE VIEW InstitutionView
AS SELECT IName,I.ALocation_ID,IPicture,AStreet,AZip,...
FROM INSTITUTION I
LEFT JOIN ADDRESS A
ON I.ALocation_ID=A.ALocation_ID;
You might think that if a JOIN is ON or WHERE "=" then there would be no ambiguity. However:
In the case of INNER JOIN, if there were no implicit conversions then columns compared equal would have the same value; but otherwise different values can compare "=". So you can't use just the column name to identify one value.
Moreover for LEFT JOIN, unmatched rows in the left table are extended by NULLs and are added to give a third intermediate result; so in a row a non-NULL in a column in one table can appear with a NULL in the same column of the other table. So again you can't use just the column name to identify one value.
Moreover there doesn't even need to be a test of equality of two columns in a JOIN, or even mention both of or either of columns with a shared name. So the result can have two columns (one from each input table) sharing a name where there is no expectation of equality.
In my schema, there are two tables with the same column names (Changing them is not an option)
Performing a query with
select * from tabA join tabB results in a mysql error of 'Duplicate column name col'
The only solution to this is to select using aliases, however I do not want to type alias.col for every column (since I need all columns from both tables)
Is it possible to do something along the lines of:
select tabA.(colA, colB, colC...), tabB.(colA, colB, colC...)
no its not possible.
you have to do like that
select tabA.colA, tabA.colB, tabA.colC..., tabB.colA, tabB.colB, tabB.colC...
if you have same name column in both tables , just give it an other alias like that.
lets say you have id column name in both tables.
select tabA.id , tabB.id as id_B
will give you result
id id_B
Linger's fiddle
I am relativly new to the SQL language. I can do a basic select, but for performance increase, I'd love to know if it is possible to merge the two queries I am doing at the moment into one.
Scenario: There are two tables. Table one has a few columns, one of them is a VARCHAR(45) named 'user', and another one is a INT which is called 'gid'. In the second table, there is a primary key column called 'gid' (INT) and a column called 'permissions' which is a TEXT column and it contains values seperated by ';'.
I have a user name, and want the text in the permissions column. The current way I do it is by fetching the gid of the first table, then doing a second query with the gid to get the permissions.
I've heard there are other ways to do this, and I have searched on Google, but I'm not sure what I should do.
EDIT:
Like this:
select t2.permissions
from table1 t1, table2 t2
where t1.user = '<SPECIFIED NAME>'
and t1.gid = t2.gid;
or you could use INNER JOIN syntax:
select t2.permissions
from table1 t1
inner join table2 t2 on t1.gid = t2.gid
where t1.user = '<SPECIFIED VALUE>'
To do this you use a JOIN. A join connects two tables in a select statement.
Like this
select *
from usertable u
join permissiontable p on u.gid = p.gid
This will give you all the columns from both tables with the id column joined. You can treat the joined table just like any table (eg select a sub-set of columns in the select list, add a where clause, etc).
You can read more about joins in any intro sql book or doing a google search.