What does ER_WARN_FIELD_RESOLVED mean? - mysql

When SHOW WARNINGS after an EXPLAIN EXTENDED shows a
Note 1276 Field or reference 'test.foo.bar' of SELECT #2 was resolved in SELECT #1
What exactly does that mean and what impact does it have?
In my case, it prevents MySQL from using what seems to be a perfectly good index. But it's not about fixing that specific query (as it is an irrelevant test).
I found http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html butError: 1276 SQLSTATE: HY000 (ER_WARN_FIELD_RESOLVED)
Message: Field or reference '%s%s%s%s%s' of SELECT #%d was resolved in SELECT #%d
Isn't much of an explanation?

You may want to use EXPLAIN in JSON format, using:
EXPLAIN FORMAT=JSON SELECT ...
It gives you a better picture how MySQL interprets the query, the JSON structure is hierarchical. But the outcome highly depends on the query and table structure.
In my case the warning was in perfectly fine WHERE clause in EXISTS subquery. This over-simplified query still produces the warning:
EXPLAIN SELECT a.id
FROM a
WHERE a.id = 1
AND EXISTS(SELECT 1 FROM c WHERE c.id = a.id)
What I figured out is, EXPLAIN was having problem with the c.id = a.id, because I already established, that a.id = 1, so the proper EXISTS, according to my ancient MySQL 5.7.9, would be:
EXISTS(SELECT 1 FROM c WHERE c.id = 1)

Related

Unknown column 'r' in field list

I've been working on a SQL query for a project, and I face an error message when I want to use it.
Here is the query itself :
SELECT COUNT(r) AS auditMade,
SUM(g.nbrMilkingCows) AS cowsAudited,
AVG(r.gainPerCowPerYearTransition) AS averageGainTransition,
AVG(r.gainPerCowPerYearLactation) AS averageGainLactation,
AVG(r.totalGain) AS averageTotalGain,
AVG(r.supplementalCostPerCow) AS averageSuppCost
FROM `smart_calculator_infos` i
INNER JOIN `smart_calculator_result` r ON r.idSmartCalculatorResult = i.idSmartCalculatorResult
INNER JOIN `calculator_general_informations` g ON g.idSmartCalculatorInfo = i.idSmartCalculatorInfo
WHERE i.idUser = 14
MySQL answers me "Unknown column 'r' in field list".
But I dont really understand why I get an error here as I define r in my INNER JOIN.
I'm kinda new at using SQL so maybe there is something pretty obvious I forgot, but I can't seem to understand what.
You can't count an alias itself, so the very first line of your query is what is causing the error:
SELECT COUNT(r)
To remedy this, you could use COUNT(*):
SELECT COUNT(*)
Or, you could count an actual column in the smart_calculator_result table, e.g.
SELECT COUNT(r.idSmartCalculatorResult)

MySQL Error Code: 1349. View's SELECT contains a subquery in the FROM clause

I am trying to create a view but get the following error:
View's SELECT contains a subquery in the FROM clause
I am running the following command. I can't seem to figure out how to substitute the nested selects with joins. Any help would be much appreciated!
create view student_fee_basic as
select fsbc.*, ffp.name, ffp.amount 'fee'
from
(select sbc.*, ffc.name 'fname', ffc.id 'fid'
from (select s.admission_no, s.first_name, bc.id 'bid', bc.code, bc.name
from (select b.id, b.name, c.code
from batches b, courses c
where b.name = '2014-2015'
and b.course_id = c.id) bc
left join students s on bc.id = s.batch_id) sbc
left join finance_fee_categories ffc on ffc.batch_id = sbc.bid
where ffc.name = 'Basic Monthly') fsbc
left join finance_fee_particulars ffp on ffp.finance_fee_category_id = fsbc.fid;
MySQL does not support subqueries in views:
Subqueries cannot be used in the FROM clause of a view.
The documentation is here.
The easiest fix is to use a series of different views for each level.
You can probably rewrite this query to remove the subqueries. However, I find it very hard to help without explicit joins.
Version 5.7 supports it.
So one way to fix it is to migrate your database to newer version
upgrade to mysql-8 and your problem is solved.

Joining derived tables

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;

Self-referencing updates in HQL

I have the following query in HQL:
update ProjectFile pf1
set pf1.validUntil.id =123
where pf1 = (
select pf from ProjectVersion pv, ProjectFile as pf
where pf.validFrom.sequence <= pv.sequence
and pf.validUntil.sequence >= pv.sequence
and pf.state <> 12
and pf.projectVersion.project.id = 1
and pv.project.id = 1
and pv.id = 12
and pf.id not in (2,3,4)
)
Hibernate parses the query correctly and generates SQL, but the database (MySQL) fails with error:
You can't specify target table 'ProjectFile' for update in FROM clause
The problem seems to be that the table to be updated is queried in the same context. Is there any way to rewrite the HQL query to produce SQL that can be executed in MySQL correctly? The other approach would be to create an intermediate table, which is what exactly I am trying to avoid.
I bumped into the same problem and posted a question here: MySQL/SQL: Update with correlated subquery from the updated table itself.
To solve your problem, you need to join at the UPDATE level, please take a look at the answer to my question.

MySQL Syntax error message "Operand should contain 1 column(s)"

I tried running the following statement:
INSERT INTO VOUCHER (VOUCHER_NUMBER, BOOK_ID, DENOMINATION)
SELECT (a.number, b.ID, b.DENOMINATION)
FROM temp_cheques a, BOOK b
WHERE a.number BETWEEN b.START_NUMBER AND b.START_NUMBER+b.UNITS-1;
which, as I understand it, should insert into VOUCHER each record from temp_cheques with the ID and DENOMINATION fields corresponding to entries in the BOOK table (temp_cheques comes from a database backup, which I'm trying to recreate in a different format). However, when I run it, I get an error:
Error: Operand should contain 1 column(s)
SQLState: 21000
ErrorCode: 1241
I'm running this in SQuirrel and have not had issues with any other queries. Is there something wrong with the syntax of my query?
EDIT:
The structure of BOOK is:
ID int(11)
START_NUMBER int(11)
UNITS int(11)
DENOMINATION double(5,2)
The structure of temp_cheques is:
ID int(11)
number varchar(20)
Try removing the parenthesis from the SELECT clause. From Microsoft TechNet, the correct syntax for an INSERT statement using a SELECT clause is the following.
INSERT INTO MyTable (PriKey, Description)
SELECT ForeignKey, Description
FROM SomeView
The error you're getting, "The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET SQL_MAX_JOIN_SIZE=# if the SELECT is okay.", is actually correct, assuming you have many rows in both BOOK and temp_cheques. You are trying to query all rows from both tables and make a cross-reference, resulting in an m*n size query. SQL Server is trying to warn you of this, before performing a potentially long operation.
Set SQL_BIG_SELECTS = 1 before running this statement, and try again. It should work, but note that this operation may take a long time.
Does B contain the UNITS column?
What is the table structure for temp_cheques and Book?
EDIT: As I said in comments, all the columns should be numeric when doing +/- and when comparing. Does the following simple SELECT work?
SELECT b.START_NUMBER+b.UNITS-1 FROM Books B
I don't have a MySQL instance handy, but my first guess is the WHERE clause:
WHERE a.number BETWEEN b.START_NUMBER AND b.START_NUMBER+b.UNITS-1;
I imagine that the MySQL parser may be interpreting that as:
WHERE number
(BETWEEN start_number AND start_number) + units - 1
Try wrapping everything in parentheses, ie:
WHERE a.number BETWEEN b.START_NUMBER AND (b.START_NUMBER + b.UNITS - 1);
The final version of the query is as follows:
Set SQL_BIG_SELECTS = 1;
INSERT INTO VOUCHER (VOUCHER_NUMBER, BOOK_ID, DENOMINATION)
SELECT a.number, b.ID, b.DENOMINATION
FROM temp_cheques a, BOOK b
WHERE a.number BETWEEN b.START_NUMBER AND (b.START_NUMBER+b.UNITS-1);
The parsing of the BETWEEN statement required parentheses, the SELECT did not, and because of the size of the two tables (215000 records in temp_cheques, 8000 in BOOK) I was breaking a limit on the select size, requiring me to set SQL_BIG_SELECTS = 1.
I ran into the same error when using Spring Repositories.
My repository contained a method like:
List<SomeEntity> findAllBySomeId(List<String> ids);
This is working fine when running integration tests against an in-memory database (h2). However against a stand alone database like MySql is was failing with the same error.
I've solved it by changing the method interface to:
List<someEntity findBySomeIdIn(List<String> ids);
Note: there is no difference between find and findAll. As described here: Spring Data JPA difference between findBy / findAllBy