I am using the below code..
SELECT
a.auction_id,
a.name,
media.media_id
FROM
auctions AS a
LEFT JOIN users AS u ON u.user_id=a.owner_id
INNER JOIN (SELECT media_url
FROM auction_media
ORDER BY media_id ASC
LIMIT 1) AS media ON a.auction_id=media.auction_id
WHERE
............
However I am getting errors such as Unknown column 'media.media_id' in 'field list'
or Unknown column 'media.auction_id' in 'on clause' - why is this?
I am sure I have used similar queries before without any problems like this.
Edit: Just tested my other query where I do something similar and I was using * as the field select. Seems I can't reference the joined sub-query table though.
Related
Hi I am writing a query where i want 2 columns from 2 different sql tables, the 2 tables are "success_ logs" and "defect_logs",The query-
SELECT DISTINCT success_logs.sl_frn_rfid_id, defect_logs.sl_frn_rfid_id
FROM success_logs
INNER JOIN defect_logs dl ON dl.frn_company_id = success_logs.frn_company_id WHERE frn_company_id="abc123";
but i am getting an error in the first line
Error Code: 1054. Unknown column 'defect_logs.sl_frn_rfid_id' in 'field list'
I am not getting where i am going wrong , most of the questions regarding the error was resolved using an alias or their issue was in the "Where" clause. Not getting where the issue is here.
If you are using an alias, I think you need to change the first line from
SELECT DISTINCT success_logs.sl_frn_rfid_id, defect_logs.sl_frn_rfid_id
to
SELECT DISTINCT success_logs.sl_frn_rfid_id, dl.sl_frn_rfid_id
Once you give the tables aliases in the FROM clause, you need to use them throughout the query. That is, defect_logs is not recognized.
I would recommend using aliases for all the tables, so:
SELECT DISTINCT sl.sl_frn_rfid_id, dl.sl_frn_rfid_id
FROM success_logs sl INNER JOIN
defect_logs dl
ON dl.frn_company_id = sl.frn_company_id
WHERE dl.frn_company_id = 'abc123';
Notes:
Only use SELECT DISTINCT if you intend to incur the overhead for removing duplicates. Usually, it is not necessary.
The standard SQL delimiter for strings is the single quote, so I recommend that you use that.
You need to qualify frn_company_id. It is in both tables, so it is not clear which column the WHERE is referring to.
The following query works just fine. I am using a value from the outer select to filter inside the inner select.
SELECT
bk.ID,
(SELECT COUNT(*) FROM guests WHERE BookingID = bk.ID) as count
FROM
bookings bk;
However, the following select will not work:
SELECT
bk.ID,
(SELECT SUM(count) FROM (SELECT COUNT(*) AS count FROM guests WHERE BookingID = bk.ID GROUP BY RoomID) sub) as sumcount
FROM
bookings bk;
The error message is: Error Code: 1054. Unknown column 'bk.ID' in 'where clause'
Why is my alias bk known in the subselect, but not in the subselect of the subselect?
For the record, I am using MySQL 5.6.
This is called "scoping". I know that Oracle (for instance) only looks out one level for resolving table aliases. SQL Server is also consistent: it looks out more than one level.
Based on this example, MySQL clearly limits the scope of the identifier bk to the immediate subquery. There is a small hint in the documentation (emphasis mine):
A correlated subquery is a subquery that contains a reference to a
table that also appears in the outer query.
However, I have not found any other specific reference to the scoping rules in the documentation. There are other answers (here and here) that specify that the scope of a table alias is limited to one level of subquery.
You already know how to fix the problem (your two queries should produce identical results). Re-arranging the query to have joins and aggregations can also resolve this problem.
Correlated Scalar Subqueries in the SELECT list can usually be rewritten to a LEFT JOIN on a Derived Table (and in many cases they might perform better then):
SELECT
bk.ID,
dt.sumcount
FROM
bookings bk
LEFT JOIN
(SELECT BookingID,SUM(COUNT) AS sumcount
FROM
(
SELECT BookingID, RoomId, COUNT(*) AS COUNT
FROM guests
GROUP BY BookingID, RoomID
) sub
) AS dt
ON bk.BookingID = dt.BookingID
I have two identical tables (apart from the name) and when trying to run the below, I get Error Code: 1054. Unknown column 'pages_jj.pageid' in 'where clause'
SELECT pages.pageid,pages.maintext FROM databasename.pages
WHERE pages.pageid=pages_jj.pageid LIMIT 50000;
Please help!
You have to add the pages_jj table to the FROM clause, like:
SELECT pages.pageid,pages.maintext FROM databasename.pages, databasename.pages_jj
WHERE pages.pageid=pages_jj.pageid LIMIT 50000;
You need to mention the two tables in your FROM clause, otherwise it will not know what pages_jj is.
SELECT pages.pageid,pages.maintext FROM databasename.pages, databasename.pages_jj
WHERE pages.pageid=pages_jj.pageid LIMIT 50000;
But be warned that this will return a cross product of the two tables. If pageid values are unique in table pages_jj , then no problem. However, if pages_jj can contain multiple lines with the same pageid, then lines from pages will be selected as many times as there are lines with the same pagesid in pages_jj... which may or may not be what you are expecting.
To get only one occurrence per matching line in pages, use the following:
SELECT pages.pageid, pages.maintext FROM databasename.pages
WHERE EXISTS (SELECT pageid FROM databasename.pages_jj WHERE pages.pageid=pages_jj.pageid) LIMIT 50000;
SELECT E.flmavailable_date,E.flmavailable_num_licenses, flmavailable_product AS SERVER
FROM (SELECT flmavailable_num_licenses,flmavailable_date
FROM licenses_available
ORDER BY flmavailable_num_licenses ASC) E
WHERE flmavailable_product <= 4;
Error:---Unknown column 'flmavailable_product' in 'field list'
Even though there is field called by that name I am getting error.
I need help to resolve this
Your outer select is trying to select a flmavailable_product from your inner select, yet there is no such field in that inner query.
Can you include the schema of the licenses_available table and a description of what your query is intended to return? This would make it easier to help you. Based on your query as it stands, it seems like you might not need that inner subquery.
You need to include the flmavailable_product field in the inner sub query if it is a column in the licences_available table, or join to the appropriate table which contains this field in the inner query :
SELECT E.flmavailable_date,E.flmavailable_num_licenses,flmavailable_product AS SERVER
FROM (SELECT flmavailable_num_licenses,flmavailable_date, flmavailable_product
FROM licenses_available ORDER BY flmavailable_num_licenses ASC)E
WHERE flmavailable_product <= 4;
Also, not sure why you are using the inner subquery as all it does is a straight select
This is the query you wanted to write.
SELECT flmavailable_num_licenses,flmavailable_date, flmavailable_product AS SERVER
FROM licenses_available
WHERE flmavailable_product <= 4
ORDER BY flmavailable_num_licenses ASC
General select query structure is:
SELECT a,b,c FROM `table`
WHERE x<=y
Here your inner query is acting as a table. However your inner query is returning only two columns: flmavailable_num_licenses & flmavailable_date.
In your outer query, you are getting an error as you are trying to select column 'flmavailable_product' which is not available in the table (Inner query).
This is clearly indicated by the error (Unknown column 'flmavailable_product' in 'field list') you are getting.
To solve this, you need to either select flmavailable_product in inner query or if it is not present there, use join to join the tables where required column is present.
Please provide table structure for exact query.
I'm trying to learn how to do subqueries, and I'm really confused at what's wrong with this simple example.
My first try was
SELECT COUNT(SELECT * FROM my_table);
but that didn't work (I guess because I need a temporary table?) so I tried this:
SELECT COUNT(items)
FROM (SELECT * FROM my_table) AS items;
Why do I get the following:
1054: Unknown column 'items' in 'field list'
You're getting the error because in this example items is a table (as it is an alias), not a column. Simplest solution is to use:
SELECT COUNT(*)
FROM (SELECT * FROM my_table) AS items
Aggregate functions (IE: COUNT, MIN, MAX, AVG, etc) only work on column references, but some accept [table].* as a parameter.