Exercises in 1-2-3, has the following form:
/* exercises: 1-2-3 update minor */
The CREATE TABLE DistrictProducts2
(district VARCHAR (16) NOT NULL,
The name VARCHAR (16) NOT NULL,
Price INTEGER NOT NULL,
Ranking INTEGER,
PRIMARY KEY (district, name));
INSERT INTO DistrictProducts2 VALUES (' northeast ', 'orange', 100, NULL);
INSERT INTO DistrictProducts2 VALUES (' northeast ', 'apple', 50, NULL);
INSERT INTO DistrictProducts2 VALUES (' northeast ', 'grapes', 50, NULL);
INSERT INTO DistrictProducts2 VALUES (' northeast ', 'lemon', 30, NULL);
INSERT INTO DistrictProducts2 VALUES (' the kanto ', 'lemon', 100, NULL);
INSERT INTO DistrictProducts2 VALUES (' the kanto ', 'pineapple', 100, NULL);
INSERT INTO DistrictProducts2 VALUES (' the kanto ', 'apple', 100, NULL);
INSERT INTO DistrictProducts2 VALUES (' the kanto ', 'grapes', 70, NULL);
INSERT INTO DistrictProducts2 VALUES (' kansai ', 'lemon', 70, NULL);
INSERT INTO DistrictProducts2 VALUES (' kansai ', 'watermelon', 30, NULL);
INSERT INTO DistrictProducts2 VALUES (' kansai ', 'apple', 20, NULL);
Topic request to insert group after Ranking, Ranking list
I write statement is as follows:
The UPDATE districtproducts2
The SET ranking=
SELECT count (d2. ` name `) + 1 as rank1
The from districtproducts2 as d1 left JOIN districtproducts2 as d2
On d1. District=d2. District and d1. Price & lt; D2. Price
GROUP BY d1. District, d1. Name
The ORDER BY d1. District, rank1
Result error:
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 'SELECT count (d2. ` name `) + 1 as rank1
The from districtproducts2 as d1 left JOIN dis' at line 3
Excuse me each bosses, why will appear this kind of circumstance, how to modify the statement?
CodePudding user response:
Select statement to add a parenthesisCodePudding user response:
Thank you bosses reply,
The UPDATE districtproducts2
The SET ranking=
(SELECT count (d2. ` name `) + 1 as rank1
The from districtproducts2 as d1 left JOIN districtproducts2 as d2
On d1. District=d2. District and d1. Price & lt; D2. Price
GROUP BY d1. District, d1. Name
The ORDER BY d1. District, rank1)
Is this right,
But to the wrong
You can 't specify target table' districtproducts2 'for update in the FROM clause
I checked the online this mistake, say need to SELECT it again:
The UPDATE districtproducts2
The SET ranking=
(
The SELECT t.r ank1 FROM
(SELECT count (d2. ` name `) + 1 as rank1
The from districtproducts2 as d1 left JOIN districtproducts2 as d2
On d1. District=d2. District and d1. Price & lt; D2. Price
GROUP BY d1. District, d1. Name
The ORDER BY d1. District, rank1) as t
)
So there is another new error:
1242 - Subquery returns more than 1 row
Aiming at this kind of mistake, some people say that home limit 1 line, but this is not the original want?
CodePudding user response: