mysql query is returning error #1054 - mysql

I'm getting this error and i don't know what to do
#1054 - Unknown column 'ple.playlists' in 'on clause'
Please help, I'm testing directly in phpMyAdmin, heres my query:
SELECT
u.`idu` AS `user_id`,
u.`username` AS `user_name`,
u.`first_name` AS `first_name`,
u.`last_name` AS `last_name`,
u.`country` AS `user_country`,
u.`image` AS `user_image`,
u.`cover` AS `user_cover`,
u.`description` AS `user_description`,
pl.`id` AS `playlist_id`,
pl.`name` AS `playlist_name`,
t.`id` AS `track_id`,
t.`title` AS `track_title`,
t.`name` AS `track_name`,
t.`art` AS `track_art`,
t.`likes` AS `track_likes`,
t.`downloads` AS `track_downloads`,
t.`views` AS `track_plays`
FROM users AS u
INNER JOIN playlists AS pl
ON u.idu=pl.by
INNER JOIN playlistentries AS ple
ON pl.id=ple.playlists
INNER JOIN tracks AS t
ON ple.track=t.id
ORDER BY `user_id` ASC, `playlist_id` ASC, `track_id` ASC

Related

Calcite Optimize cause duplicated column name error

We use calcite to execute a mysql query, the SQL is:
select a.title, c.score from phx_ep.phx_gql_product a left join phx_ep.phx_gql_product_tag c on a.product_id = c.product_id limit 3
It's simple and clear, but calcite optimized it to:
SELECT `t2`.`title`, `t2`.`score`
FROM (SELECT *
FROM (SELECT `product_id`, `title`
FROM (SELECT *
FROM `phx_ep`.`phx_gql_product`
LIMIT 3) AS `t`) AS `t0`
LEFT JOIN (SELECT `product_id`, `score`
FROM `phx_ep`.`phx_gql_product_tag`) AS `t1` ON `t0`.`product_id` = `t1`.`product_id`
LIMIT 3) AS `t2`
Which caused a Duplicate column name error
If remove 'limit 3', it works well.
Is it a bug?

Error: SQLSTATE[42S22]: Column not found: 1054

When I run my baked table (WampServer MySQL database), I get this error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Users.user_id' in 'on clause'
If you are using SQL keywords as table column names, you can enable identifier quoting for your database connection in config/app.php.
SQL Query:
SELECT `Bookmarks`.`id` AS `Bookmarks__id`,
`Bookmarks`.`user_id` AS `Bookmarks__user_id`,
`Bookmarks`.`title` AS `Bookmarks__title`,
`Bookmarks`.`description` AS `Bookmarks__description`,
`Bookmarks`.`url` AS `Bookmarks__url`,
`Bookmarks`.`created` AS `Bookmarks__created`,
`Bookmarks`.`modified` AS `Bookmarks__modified`,
`Users`.`id` AS `Users__id`,
`Users`.`email` AS `Users__email`,
`Users`.`password` AS `Users__password`,
`Users`.`created` AS `Users__created`,
`Users`.`modified` AS `Users__modified`
FROM `bookmarks` `Bookmarks`
INNER JOIN `users` `Users` ON
`Users`.`user_id` = (`Bookmarks`.`user_id`)
LIMIT 20 OFFSET 0
If you want to customize this error message, create src\Template\Error\pdo_error.ctp.
In your user table, primary id is id not user_id; that is why you're getting an error. Try this query instead:
SELECT `Bookmarks`.`id` AS `Bookmarks__id`,
`Bookmarks`.`user_id` AS `Bookmarks__user_id`,
`Bookmarks`.`title` AS `Bookmarks__title`,
`Bookmarks`.`description` AS `Bookmarks__description`,
`Bookmarks`.`url` AS `Bookmarks__url`,
`Bookmarks`.`created` AS `Bookmarks__created`,
`Bookmarks`.`modified` AS `Bookmarks__modified`,
`Users`.`id` AS `Users__id`,
`Users`.`email` AS `Users__email`,
`Users`.`password` AS `Users__password`,
`Users`.`created` AS `Users__created`,
`Users`.`modified` AS `Users__modified`
FROM `bookmarks` `Bookmarks`
INNER JOIN `users` `Users` ON
`Users`.`id` = `Bookmarks`.`user_id`
LIMIT 20 OFFSET 0

Second Subquery reference to parent-parent query: column not found

Is there a limit of subquerys where the referencing outer column is working on?
I tried this query:
SELECT
`userouter`.`id` AS `user_id`
FROM
`users` AS `userouter`
WHERE
123456 = (
SELECT
SUM(`tmp`.`sum`) AS `sum_total`
FROM
(
SELECT
SUM(`invoiceposition`.`number` * `invoiceposition`.`amount`) AS `sum`
FROM
`invoices` AS `invoice` INNER JOIN
`invoicepositions` AS `invoiceposition` ON
`invoice`.`id` = `invoiceposition`.`invoice`
WHERE
`invoice`.`user` = `userouter`.`id`
GROUP BY
`invoice`.`id`
) AS `tmp`
)
GROUP BY
`userouter`.`id`
And i get Error Code: 1054. Unknown column 'userouter.id' in 'where clause'
How can i reference the userouter.id in the sub-sub query?
As it seems in a normal way not possible to resolve the problem (double nested subquery reference to outer query), i now solved the problem by creating a mysql function with the user id as parameter.
hope this will help other searchers
Remove the double nesting.
SELECT
`userouter`.`id` AS `user_id`
FROM
`users` AS `userouter`
LEFT JOIN (
SELECT
`invoice`.`user` as `user_id`, SUM(`invoiceposition`.`number` * `invoiceposition`.`amount`) AS `sum`
FROM
`invoices` AS `invoice` INNER JOIN
`invoicepositions` AS `invoiceposition` ON
`invoice`.`id` = `invoiceposition`.`invoice`
WHERE
`invoice`.`user` = `userouter`.`id`
GROUP BY
`invoice`.`id`
) AS `tmp`
ON `tmp`.`user_id` = `userouter`.`id`
WHERE
123456 = `userouter`.`id`
GROUP BY
`userouter`.`id`

MySQL Unknown column in having clause

I'm trying to get all the wins per team, however, SQL decides to throw an error
The following query is being executed:
SELECT `t`.`teamcode`, COUNT(*) AS `gewonnen`
FROM `Team` `t`
INNER JOIN `Wedstrijd` `w` ON `w`.`teamthuis` = `t`.`teamcode`
GROUP BY `w`.`teamthuis`
HAVING `w`.`scorethuis` > `w`.`scoreuit`
#1054 - Unknown column 'w.scorethuis' in 'having clause'
Without aliases:
SELECT `Team`.`teamcode`, COUNT(*) AS `gewonnen`
FROM `Team`
INNER JOIN `Wedstrijd` ON `Wedstrijd`.`teamthuis` = `Team`.`teamcode`
GROUP BY `Wedstrijd`.`teamthuis`
HAVING `Wedstrijd`.`scorethuis` > `Wedstrijd`.`scoreuit`
#1054 - Unknown column 'Wedstrijd.scorethuis' in 'having clause'
There is no need to use HAVING. Try WHERE instead:
SELECT `t`.`teamcode`, COUNT(*) AS `gewonnen`
FROM `Team` `t`
INNER JOIN `Wedstrijd` `w` ON `w`.`teamthuis` = `t`.`teamcode`
WHERE `w`.`scorethuis` > `w`.`scoreuit`
GROUP BY `w`.`teamthuis`
I think if you had select w.scorethuis w.scoreuit both in select statement then 'Having;' would work. But I faced the same problem and I resolved it by the above way.
SELECT `t`.`teamcode`, COUNT(*),***`w`.`scorethuis`, `w`.`scoreuit`*** AS `gewonnen`
FROM `Team` `t`
INNER JOIN `Wedstrijd` `w` ON `w`.`teamthuis` = `t`.`teamcode`
GROUP BY `w`.`teamthuis`
HAVING `w`.`scorethuis` > `w`.`scoreuit`

Magento Store - SQL Error

I get this error in my Magento store when I turn on flat catalogs:
SELECT COUNT(_table_views.event_id) AS `views`, `e`.*, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`category_ids`, `e`.`created_at`, `e`.`enable_googlecheckout`, `e`.`has_options`, `e`.`image_label`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`weight`, `e`.`weight_type`, `e`.`display_price_group_0`, `e`.`display_price_group_1`, `e`.`display_price_group_2`, `e`.`display_price_group_3`, `e`.`display_price_group_4`, `cat_index`.`position` AS `cat_index_position` FROM `report_event` AS `_table_views`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = _table_views.object_id AND e.entity_type_id = 10
INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.category_id='10' WHERE (_table_views.event_type_id = '1') AND (logged_at >= '2010-01-06 17:03:57') AND (logged_at <= '2010-01-13 17:03:57') GROUP BY `e`.`entity_id` HAVING (views > 0) ORDER BY `views` desc LIMIT 10
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'e.enable_googlecheckout' in 'field list'
Does anyone know how I would go about fixing this?
Describe the table catalog_product_entity using
describe catalog_product_entity;
and see if enable_googlecheckout is present in the table as a column. If not present you can try taking it out of the query as:
SELECT COUNT(_table_views.event_id) AS `views`, `e`.*, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`category_ids`, `e`.`created_at`, `e`.`has_options`, `e`.`image_label`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`weight`, `e`.`weight_type`, `e`.`display_price_group_0`, `e`.`display_price_group_1`, `e`.`display_price_group_2`, `e`.`display_price_group_3`, `e`.`display_price_group_4`, `cat_index`.`position` AS `cat_index_position` FROM `report_event` AS `_table_views`
INNER JOIN `catalog_product_entity` AS `e` ON e.entity_id = _table_views.object_id AND e.entity_type_id = 10
INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='1' AND cat_index.category_id='10' WHERE (_table_views.event_type_id = '1') AND (logged_at >= '2010-01-06 17:03:57') AND (logged_at <= '2010-01-13 17:03:57') GROUP BY `e`.`entity_id` HAVING (views > 0) ORDER BY `views` desc LIMIT 10