Insert with multiple select statement if not exist - mysql

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);

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 update subquery can't specify table target issue

I have been going through many queries, but can't seem to find the right combination to get this to work. I get the error "You can't specify target table 't1' for update in FROM clause". I know MySQL doesn't like the subquery in the update statement and have read about wrapping it in other select statements, but can't seem to figure it out. Here is a stripped down query of what I am looking for:
UPDATE myTable t1 SET t1.num=concat(t1.num,'B') WHERE t1.num in ('1','2') and t1.expiry=(SELECT max(t2.expiry) from myTable t2 where t2.num=t1.num);
Basically trying to get the latest date (expiry) for each number (num) and change the number where applicable.
This should works :
UPDATE myTable t1, (SELECT num, max(expiry) expiry from myTable t2 group by num) t2
SET t1.num = concat(t1.num,'B')
WHERE t1.num in ('1','2')
and t1.expiry = t2.expiry
and t1.num = t2.num;

Problem in where clause and aliases in MySQL

What's wrong with this mysql query :
select *
from tpa as t1
where ( select count(*)
from tpa as t2
where t1.id = t2.id )
error :
Error Code: 1054. Unknown column 't1.id' in 'field list'
I think, as Cfreak pointed out in comment, that the alias is not visible in the subquery.
I think also that you forgot to specify some condition for your count(*) result to be equal to some-number (or other condition):
select * from tpa as t1
where
(
select count(*) from
(
select * from tpa
)
as t2
where t1.id = t2.c_id
) = 1
Change "= 1" with any numery condition you like, or this would be a silly way to rewrite this much simpler query:
select distinct * from tpa
:-)
select id, count(*) from tpa as t1
group by id
/*If you need a certain count.. add-*/
having count(*) > 2
You should not need to use a subquery in either case.

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

Subquery error in MySQL with max()

I'm trying a subquery in MySQL using max(), and I keep running into an error. The gist of the query is below (though I've changed the field names).
select table1.field1, table1.field2, table2.field3, table2.field4, table3.field5,
(select max(age)
from age_table
where age_table.person = table2.person)
from table1
inner join table2 on table2.person = table1.person
inner join table3 on table3.person = table1.person
inner join age_table on age_table.person = table1.person
When I try this, I get a syntax error that points to
'from age_table where age_table.person=table2.person'
...but I can't figure out what the problem is.
Use table aliases to differentiate between tables, without having to use the full table name:
SELECT t1.field1, t1.field2, t2.field3, t2.field4, t3.field5,
(SELECT MAX(at.age)
FROM AGE_TABLE at
WHERE at.person = t2.person) AS max_age
FROM TABLE1 t1
JOIN TABLE2 t2 ON t2.person = t1.person
JOIN TABLE3 t3 ON t3.person = t1.person
I removed what appeared to be a redundant JOIN to the AGE_TABLE, seeing as it wasn't used in the SELECT clause.
It's also good habit to define a column alias for derived column values - makes them easier to reference. See "max_age" for an example.
You need to create an alias for your subquery eg:
(select max(age) from age_table where age_table.person = table2.person) temp
and leave rest of the things as they are.