Im in a position where I cannot alter the table structure of my database and I have Ambiguous Column Names in [table1] and [table2]. I do not need to use any fields from [table2] but its existence is necessary to relate to another table. Is there a way that I handle this?
Every time you refer to one of the ambiguous column names you should specify the table name or alias.
SELECT ...
FROM [table1]
JOIN [table2]
ON [table1].ambiguous_column = [table2].ambiguous_column
AND ...
use table aliases
SELECT A.*
FROM TABLE_A A
JOIN TABLE_B B ON A.ID = B.ID
ORDER BY A.FIELD
use the SQL statement AS to create uniquel names
SELECT
A.feld1 AS F1,
A.feld2 AS F2,
B.feld1 AS F3
FROM table1 AS A
JOIN table2 AS B ON A.id = B.id
ORDER BY A.field1
Related
In a MySQL database with two tables table_A and table_B I want to return selected row columns from table_A based on comparison with values in table_B. The below erroneous line sums up the idea:
SELECT col_1, col_2, col_3 FROM table_A where table_A.col_1 = table_B.col_2;
I do not want any elements from table_B.
Why I can't use a left outer join: I've tried this with a left outer join as illustrated here(https://blog.codinghorror.com/a-visual-explanation-of-sql-joins/) however the database complains that the column names are ambiguous and changing table column names in the database isn't an option.
I you want rows in table_A whose col1 can be found in table_B(col_2), you can use exists:
select a.col_1, a.col_2, a.col_3
from table_A a
where exists (select 1 from table_B b where b.col_2 = a.col_1);
If you want rows that do not exist in table_B, then just change exists to not exists.
Note that I prefix the column names with the (alias of the) table they belong to. This is called qualifying the columns, and is how you avoid the ambiguous column name problem that you seemingly have met when trying to join.
If column names are ambiguous, qualify them, eg
select table_A.col_1, table_A.col_2, table_A.col_3
from table_A
join table_B on table_A.col_1 = table_B.col_2
or for brevity you can assign an aliases to tables:
select a.col_1, a.col_2, a.col_3
from table_A a
join table_B b on a.col_1 = b.col_2
I want to join two tables with different data and data type is same.
In tableA the column col1 is with varchar datatype i.e. 123 and in tableB the column col1 is with varchar datatype i.e. ABC-123
Is there any way to join both columns by adding ABC as prefix to col1 in table 1 or by removing prefix ABC from col1 table 2.
You can use CONCAT(), as in:
select *
from table_a a
join table_b b on concat('ABC-', a.col1) = b.col2
This issue is quite common, specially in old databases, where you need to join VARCHAR columns with NUMERIC ones, since the designers back in the 90s though it that way.
Just use function CONCAT() at the ON clause of INNER JOIN
select * from
tableA a inner join tableB b
on CONCAT('ABC-', a.col1) = b.col2
Simply, I want to find a solution for testing if one column value exits within another, or not, across tables. I've naturally jumped to 'NOT IN clause' but I've also discovered I can't use a colum name within the bracket (b.full_name)
SELECT
*
FROM
tbl1_name a
INNER JOIN
tbl2_name
ON a.id = b.id
WHERE
a.name NOT IN (b.full_name);
What clause can I use to achieve what I'm looking for etc.
You could simply do
SELECT * FROM tbl1_name a
INNER JOIN tbl2_name ON a.id = b.id
WHERE a.name <> b.full_name;
The IN keyword is normally used to check for multiple values example
NOT IN ('A', 'B');
Or
NOT IN (subquery);
You can use subquery after not in clause and select all the names from the table b. Hope it will resolve your problem.
Select * from table1 a inner join table2 b
on a.id=b.id
where a.name not in
(Select names from table2)
you can perform that using subqueries, this way :
SELECT * FROM tbl1_name a
WHERE a.name IN (SELECT b.full_name FROM tbl2_name b WHERE a.id = b.id);
I have 2 MySQL tables A and B.
I would like to select only the records from B where a certain value exists in A.
Example:
A has columns: aID, Name
B has columns: bID, aID, Name
I just want the records from B for which aID exists in A.
Many thanks.
You need to do either INNER JOIN - records that exists in both tables, or use LEFT join, to show records that exists in A and matching IDs exists in B
A good reference:
You need to make a join, and if you don't want to retrieve anything from table b, just return values from table a.
This should work
select b.* from b join a on b.aID=a.aID
Below query will also work and will be effective
SELECT * FROM B
WHERE B.aID IN (SELECT DISTINCT aID FROM A)
You just need a simple inner join between tables A and B. Since they are related on the aID column, you can use that to join them together:
SELECT b.*
FROM tableB b
JOIN tableA a ON a.aID = b.aID;
This will only select rows in which the aID value from tableB exists in tableA. If there is no connection, the rows can't be included in the join.
While I recommend using a join, you can also replace it with a subquery, like this:
SELECT *
FROM tableB
WHERE aID NOT IN (SELECT aID FROM tableA)
You can use join like this.
Select b.col1,b.col2... From tableB b inner join table tableA a on b.field = a.field
Have you tried using a LEFT JOIN?
SELECT b.* FROM tableB b LEFT JOIN tableA a ON b.aID = a.aID
I have two tables with exactly the same fields. Table A contains 7160 records and table B 7130 records.Now I want to insert distinct records from table A into table B such that B should not have any duplicate entry in it. How should I go about doing this?
This basically selects records that are in A that are not in B. It would work, but you might have to tweak the field you use to uniquely identify a record. In this example I used field 'ID' but you might have to change that to A.field1 = B.field1 AND A.field2 = B.field2 etc.
INSERT INTO TABLEB
(
SELECT A.*
FROM TABLEA A
LEFT JOIN TABLEB B ON A.ID = B.ID
WHERE B.ID IS NULL
)
You can use a "union" query to combine the results from multiple tables into a single result set. "union" will only return distinct rows from all tables.
See this page for more info:
http://www.tutorialspoint.com/mysql/mysql-union-keyword.htm
insert into tableB (id)
select t1.id from tableA t1
where t1.id not in (select t2.id from tableB t2)