SQL query from multiple table with condition - mysql

Using MySQL, I want to do all these in one insert:
table T1 contains column Ca & Cb. T1 is in database D1. Insert into table T1 specifying column Ca, Cb.
table T2 contains column C2. T2 is in database D2. Set T1.Ca's value with T2.C2
table T3 contains column C3 & C4. T3 is in database D2. use the T2.C2 value to query T3.C3 and use the C4 value of the same row to set T1.Cb
so I did:
insert into T1(Ca, Cb)
select C2
from D2.T2
union
select C4
from D2.T3
where C3=T2.C2;
Error Code: 1054. Unknown column 'T2.C2' in 'where clause'
Please help. Thank you in advance.

If i understand correcly you should use a join and not an union
insert into D1.T1(Ca, Cb)
select T2.C2, T3.C4
from D2.T2
INNER JOIN D2.T3 on T2.C2 = T3.C3
the second select of the union don't know the content of the firts select .. so you have the error
Unknown column 'T2.C2' in 'where clause'

Queries which are Unioned are independent of one another.
You want to join these tables.
select C2,C4
from D2.T2 a
INNER JOIN D2.T3 b ON b.C3=a.C2;

Related

#1054 - Unknown column in 'where clause'

I have this query in mysql database:
INSERT INTO `table1`( `text1`, `text2`, `link` )
SELECT
`text1`,
`text2``,
`link`
FROM `table2`
WHERE `table1`.`code` = `table2`.`code`;
I get an error:
#1054 - Unknown column 'table1.code' in 'where clause'
What am I doing wrong? I have no aliases, I tried HAVING instead of WHERE, I tried INNER JOIN but no success. My code columns have no indices.
First of all you should show also the structure of table2 to let us help you better.
Otherwise, the error is due to the lack of table1 in your query.
As code is not present in your INSERT statement I may assume that it is an autoincrement field.
Your query (I suppose, as I can't see the definition of table2, as wrote before), could be
SELECT
t2.text1,
t2.text2,
t2.link
FROM table2 AS t2
INNER JOIN table1 AS t1 ON (t1.code = t2.code)
;
If the link between table1 and table2 is 1-to-many or many-to-many, just add a DISTINCT after the SELECT keyword to avoid duplicated results or change your query in
SELECT
t2.text1,
t2.text2,
t2.link
FROM table2 AS t2
WHERE EXISTS (
SELECT 'x'
FROM table1 AS t1
WHERE t1.code = t2.code
)

mysql error says unknown column in 'field list', how do i select data in this column?

I created two temp tables to join them.
create temporary table if not exists dbo.t1 as
(select * from dbo.cp where id%2=1);
create temporary table if not exists dbo.t2 as
(select * from dbo.cp where id%2=0);
I have two columns in a temporary table:
When I query
select * from dbo.t1;
this table comes up.
codes id
123 1
213 2
144 3
423 4
My issue: When i say query
select codes from dbo.t1;
I get the error: unknown column 'codes' in fieldlist.
when i query
select 'codes` from dbo.t1
I get the output
codes id
codes 1
codes 2
codes 3
codes 4
when i query
select `codes` from dbo.t1
I get the output unknown column in field list.
This is a huge issue because when I try these different queries to do an inner join I do not get the correct output:
Create Table edit AS
(select
't1.codes',
t1.id t1_id, t2.*
from t2
inner join t1 on t1.id = t2.id - 1);
Create Table edit AS
(select
t1.codes t1_codes,
t1.id t1_id, t2.*
from t2
inner join t1 on t1.id = t2.id - 1);
Create Table edit AS
(select
t1*, t2.*
from t2
inner join t1 on t1.id = t2.id - 1);
The issue here is I get the error of "duplicate column name codes"
Try to wrapped your code with backtick ( ` ) symbol
SELECT `codes` FROM ..
TICKS NOT QUOTES
select `codes` from dbo.t1;

Insert with multiple select statement if not exist

I'd tried insert with multiple statement with the code below
insert into peoplePos
select a.name,b.option
FROM (SELECT name from people t1) a
JOIN (SELECT option FROM optionTable WHERE name = 'Position') b
where not exists (select * from peoplePos t2 where t2.name = t1.name);
However i got this error
Error Code: 1054. Unknown column 't1.name' in 'where clause'
It seems in the where clause, t1 cant access the people t1 declared earlier
I'd tried using a.t1.name and people.name, both doesn't work
Is there away to access it? Thanks
I'd think you'd want some conditions on your join, but for what you ask, t1 does not exist. You have aliased it as a so use a.
insert into peoplePos
select a.name,b.option
FROM (SELECT name from people t1) a
JOIN (SELECT option FROM optionTable WHERE name = 'Position') b ON "SOME CONDITION OR OTHER"
where not exists (select * from peoplePos t2 where t2.name = a.name);

join all columns in table1 to columns with "unique names" in table2

I am using mySQL 5.6. I have two tables: t1 and t2. Both have many columns. And many columns in t1 and t2 share the same name: for example, there is a "var1" column in t1 and in t2.
I want to join the tables, selecting (a) all columns from t1 and (b) only the columns in t2 that have names that don't appear in t1. For example, I would not select "var1" from t2.
Here is a valid mySQL command that does not work because some columns share the same name:
SELECT * FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
MySQL sensibly returns this error message:
ERROR 1060 (42S21): Duplicate column name 'var1'
So I want to run a command like
SELECT t1.*, DISTINCTCOLUMNS(t2.*) FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
Except, of course, that there is no DISTINCTCOLUMNS operation in SQL. But is there a similar (real) command that will achieve the same effect?
I see that common advice is to avoid SELECT * syntax, partly for efficiency purposes. I appreciate that, but I do not want to write out the names of all of the columns that I need.
The real issue here is you have the same column name for both tables. So you need to alias your columns.. This is also why you shouldn't just pull all columns out but the specific ones you need..
SELECT t1.ID as t1_id,
t1.var1 as t1_var1,
t2.ID as t2_id,
t2.var1 as t2_var1,
... Etc.
FROM t1
LEFT JOIN t2 on t1.ID = t2.ID
With two ID columns and no way to distinguish between the two an error will occur
You could qualify the columns like
SELECT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
But you should not do it unless the columns with identical names do actually hold/reference the same data.
try like following. this may help you
SELECT t1.*, t2.YourDesiredColumnName FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);

mysql subquery field application Range

Here is my code:
SELECT field1, field2,
(SELECT * FROM table1 WHERE a = field2),
(SELECT COUNT(*)
FROM (SELECT *
FROM table2
WHERE c = field2) as t1) as count
FROM table3;
...and here is error message:
/* SQL Error (1054): Unknown column
'field2' in 'where clause' */
I want to execute one query, to get table2's total counts.
The problem is that you're trying to use a variable declared in the outer query in the inner query. Variables are scoped the opposite way in SQL, you only have access to inner queries. What you want to do (I believe) is look for the number of table2 that have a c that matches the a from table1. This should give you that answer.
SELECT
table1.a,
table2.c,
count(*)
FROM
table1
JOIN
table2 ON table2.c = table1.a
GROUP BY
table1.a
I re-wrote your query as:
SELECT t3.field1,
t3.field2,
t1.*,
t2.cnt
FROM TABLE3 t3
LEFT JOIN TABLE1 t1 ON t1.a = t3.field2
LEFT JOIN (SELECT t.c,
COUNT(*) AS cnt
FROM TABLE2 t
GROUP BY t.c) t2 ON t2.c = t3.field2
The 1054 error is due to referencing field2 two subqueries deep - most only support one level deep.
UPDATE:
I want to edit the answer, since in MySQL 8.0 your query is already correct, derived tables can access external references no matter the level.
SELECT field1, field2,
(SELECT 'hola' FROM table1 WHERE a = field2),
(SELECT COUNT(*)
FROM (SELECT *
FROM table2
WHERE c = field2) as t1) as count
FROM table3;
"Prior to MySQL 8.0.14, a derived table cannot contain outer references. This is a MySQL restriction that is lifted in MySQL 8.0.14, not a restriction of the SQL standard. For example, the derived table dt in the following query contains a reference t1.b to the table t1 in the outer query:" https://dev.mysql.com/doc/refman/8.0/en/derived-tables.html