Joining derived tables - mysql

EDIT: I am using phpMyAdmin interface, and I have been copy/paste the codes from phpMyAdmin to here. The phpMyAdmin seems to run a "different code" as I run the following code, and generating some error message that are referring to that "different code", causing huge confusion.
** Final edit: It seems Safari is causing this: it run the "different query" when I try to run 2nd query below. Use Firefox instead, and it generate correct results. Thanks for the help and sorry for the confusion. **
I have two tables: newsFeeds, comments, where
** newsFeeds contains column PID, comments contains column FID.**
I want to join rows in two tables with matching PID = FID. My code is:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
)
ON comments.FID = newsFeeds.PID
and the error message is "#1248 - Every derived table must have its own alias".
I then add in AS newtb after ) according to other posts here. And the code is then:
SELECT * FROM newsFeeds
INNER JOIN
(
SELECT * FROM
comments
) AS newtb
ON newtb.FID = newsFeeds.PID
But another error shows up: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN( SELECTFID,COUNT(*) AScount FROMcomments
LIMIT 0, 25' at line 8
I wonder how to correctly do this?

You should correct this by removing the derived table:
SELECT *
FROM tb_1 INNER JOIN
tb_2
ON tb_2.FID = tb_1.PID;
MySQL has a tendency to materialize derived tables, which hurts performance.
The answer to your question, though, is to add a name after the parentheses:
SELECT *
FROM tb_1 INNER JOIN
(SELECT *
FROM tb_2
) t2
ON t2.FID = tb_1.PID;

Related

DELETE FROM throws a SQL Error [1064] [42000]: (conn=5159) on a query, on which a SELECT works

Trying to delete a specific amount of rows in a MySQL query, I am able to SELECT whatever I want to delete with the following command, getting the results I need:
select * from ns_cos ns where ns.created_at <>
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
However, when I try to delete the selected data with:
delete from ns_cos ns where ns.created_at not exists
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
I get:
SQL Error [1064] [42000]: (conn=5159) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ns where ns.created_at not exists (select max(nsa.created_at) from ns_cos nsa wh' at line 1
What am I doing wrong?
Your immediate issue is that not all MySQL versions support aliasing the table directly in delete from. Furthermore, though, you cannot re-open the table you delete from in the from clause.
Consider using the delete ... join syntax.
delete ns
from ns_cos ns
inner join (
select month_year, max(nsa.created_at) created_at
from ns_cos nsa
group by month_year
) ns1 on ns1.month_year = ns.month_year and ns1.created_at <> ns.created_at
EXISTS in there not posible use IN clause, but you need to enclose the table in a seprate select, so that mysql thinks it is another table
delete from ns_cos ns
where ns.created_at not IN (select max(nsa.created_at) from (SELECT * FROM ns_cos) nsa where nsa.month_year = ns.month_year)
This happens when you join tables from two schemas, no matter whether you take aliases for the table names or not.
Error
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '...'
delete !ALIAS! from table ALIAS ...
Strangely, only with aliases, you can get around this error in a one-step SQL. Tested in MySQL 5.7.
Try this pattern, using the alias of the table you want to delete from between delete and from:
delete t1 from table t1
join table2 t2
on t1.id = t2.id
Your code would be:
delete ns from ns_cos ns where ns.created_at not exists
(select max(nsa.created_at) from ns_cos nsa
where nsa.month_year = ns.month_year)
Other steps I checked before finding out the alias trick (do not read)
I tried it with a view of the other schema's table in the same schema, that is not enough.
One way to get around this is to make a copy of the table in the same schema (and delete that copy afterwards).
You might also somehow make a full "linked server link" like in T-SQL [linkedservername].DB1.Schema.Table1 at How to join two tables if they are in different schemas Your code is on the same server, but this might still help jumping over to the other schema, untested.

MySQL trouble connecting two columns of names

I have two tables that I'm trying to connect. One table is called 2019projections and the other is called 2019actualstat. I want to connect the two by names. I'm 99% sure every name that is in 2019actualstat is in 2019projections, but not every name in 2019actualstat is in 2019projections. The latter has alot more names, but most of them are useless.
I've tried left join and right join.
I've tried select distinct
I gave a shot at exists
This is what I have so far:
USE Fantasyfootball;
SELECT DISTINCT *
FROM 2019actualstat;
LEFT JOIN 2019projections ON
2019actualstat.Player =
2019projections.first_last;
It's giving me the 1064 error, but I think it has to do with the 2019projections table having more records.
21:27:26 LEFT JOIN 2019projections ON 2019actualstat.Player =
2019projections.first_last Error Code: 1064. You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'LEFT JOIN 2019projections ON
2019actualstat.Player = 2019projections.first_last' at line 1 0.00071
sec
2019projections.first_last is a varchar(50) and 2019actualstat.player is text
PS: I have the .csv files which I'm not sure how to post, but I would be happy to send them both.
You're missing the select list, and have a redundant (wrong) semicolon at the end of the from clause:
SELECT *
FROM 2019actualstat
LEFT JOIN 2019projections ON 2019actualstat.Player = 2019projections.first_last;

How to resolve wrong syntax near HAVING COUNT DISTINCT?

This it the code I am trying to execute:
SELECT ID_K
FROM koncert,
programi
WHERE koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT (DISTINCT programi.Salla) = 2
It returns this error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to
your MariaDB server version for the right syntax to use
near 'DISTINCT programi.Salla)=2 LIMIT 0, 25' at line 4.
Tried to change different things but it still won't work .
You should use the count(DISTINCT programi.Salla
) and not count (..) ..remove space between COUNT and (...
SELECT koncert.ID_K
FROM koncert
INNER JOIN programi on koncert.ID_K = programi.ID_K
GROUP BY koncert.ID_K
HAVING COUNT(DISTINCT programi.Salla) = 2
but you need also tablename for avoid ambiguity and use explicit join sintax too
First you should use qualified name for your column, when column name is same in both table
SELECT ID_K FROM
should be
SELECT programi.ID_K FROM
else, you will get ambiguous column error. Otherwise, your query looks fine except removing extra space when calling COUNT (#spencer already mentioned in comment)
Also, it is good practice to join your table using JOIN (or INNER JOIN, LEFT JOIN etc) keyword, which makes your query more clear and readable.

MySql subquery error with delete statement

I'm learning sql from these wikibooks pages and I'm trying to answer the last question "Remove all boxes from saturated warehouses" using mysql syntax. The following is what I came up with on my own mainly using previous answers from the same page.
delete from Boxes as b
where b.Warehouse in (
select w.Code
from ( select *
from Warehouses) as w
where w.Capacity < ( select count(*)
from Boxes bb inner join Warehouses ww
on bb.Warehouse = ww.Code
group by bb.Contents) ) ;
The query for this question on the site is generating
this solution doesn't work with mysql 5.0.67
ERROR 1093 (HY000): You can't specify target table 'Boxes' for update in FROM clause
That's why I'm using multiple sub queries as a way to come around this mysql message. However, I have an error in my query and therefore it's not running.
Error Code: 1064. You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'as b where b.Warehouse in ( select w.Code
from ( sel' at line 1
Any idea?
I assume Boxes and Warehouse are tables, you gave Boxes an alias and used it on the Warehouse table, that's probably why it didn't work. Maybe you should try with a query after 'delete from'.
I think your warehouses table returns from than one columns (since * is used). Replace * with the column u want to get the output from.

Delete duplicates for multiple columns in JOIN on same table

I am trying to make a delete from joined same table like this:
DELETE FROM `sp10_seo_url` AS sp1 JOIN
(
SELECT seo_url_pk, COUNT(*) AS maxc
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING maxc > 1
) AS sp2
ON sp1.seo_url_pk = sp2.seo_url_pk
However I am getting a mysql error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS sp1 JOIN ( SELECT seo_url_pk, COUNT(*) AS maxc FROM `sp10_s' at line 1
And I am not sure at all where the error is. The inner query runs just fine and returns the expected set of results. The "ON" keys are properly named (same since we are talking about the same table).
I guess the idea of the query is pretty clear (clean the table of different rows have the same set of values for the three "group by" columns. Is there another way to do this?
Thanks!
you can "cheat" mysql with a double indirection (as explained here Deleting a row based on the max value):
delete from `sp10_seo_url`
where seo_url_pk in (
select seo_url_pk from (
SELECT seo_url_pk
FROM `sp10_seo_url` sp1,
(
SELECT seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
FROM `sp10_seo_url`
GROUP BY seo_url_entity_type, seo_url_entity_id, seo_url_language_fk
HAVING count(*) > 1
) sp2
where sp1.seo_url_entity_type = sp2.seo_url_entity_type
and sp1.seo_url_entity_id = sp2.seo_url_entity_id
and sp1.seo_url_language_fk = sp2.seo_url_language_fk
) t
);
http://sqlfiddle.com/#!2/899ff5/1