soft genic
soft genic

Reputation: 2056

Insert not inserting all

Following is my query in which I am trying to insert those records whose name doesnot exist in food (insertion table) . For id generation i am trying to get the count from food and adding one to it COUNT( food.FOOd_id ) +1 eg. Chineese_01 and then comparing the name coming from pick table with foodname of food table. In order to allow inseertion for those names which doesnot already exist. The following query gives right result but it make insertion once (one row) and when i run it again it again makes one insertion and so on. Kindly let me know how can i modify the following query so it should make insertion for all. Thanks

    INSERT IGNORE into food(FOOD_id,FOOD_name)
    Select CONCAT(  FOOD,"_", COUNT( food.FOOd_id ) +1 ),CONCAT(  Typ,CONCAT('(' ,family,')' )) from pick,food
    where CONCAT(  Typ,CONCAT('(' ,family,')' )) NOT IN (Select FOOD_name from food )

SQL FIDDLE: (For testing and making the right query)

http://sqlfiddle.com/#!2/91408/1

Upvotes: 1

Views: 73

Answers (1)

PinnyM
PinnyM

Reputation: 35531

The problem is that COUNT() in the SELECT clause is forcing aggregation, so only one row will be selected. You need to either specify a grouping, or move it to a subquery. Adding this to the bottom of the query is one way to solve it:

GROUP BY pick.food, pick.typ, pick.family

Upvotes: 1

Related Questions