Jump to content

mysql query


bugcoder

Recommended Posts

I have three columns table tbl_foods with sample data as follows

 

id food_id weight

1  1050 cup, tbsp

2  1050 oz

3  1053 tbsp

4  1058 cup

5  1058 cup,oz

6  1069 oz

7  1070 tbsp

8  1071 fluid

9  1078 tbsp

 

 

I want every food_id which does not have "oz" as weight should be inserted with new id in this table

e.g now 1053,1070,1071 and 1078 will be inserted with new ids and wights as "oz".

Link to comment
Share on other sites

For this table assuming it's name is "food"

 

id food_id weight
1   1050   cup, tbsp
2   1050   oz
3   1053   tbsp
4   1058   cup
5   1058   cup,oz
6   1069   oz
7   1070   tbsp
8   1071   fluid
9   1078   tbsp

 

Try

 

SELECT * FROM `food` WHERE `weight` NOT LIKE '%oz%';

Link to comment
Share on other sites

Assuming you are going to insert back into the same table, and the field "ID" is set as Primary Index, AutoIncrement, you can do the following

 

INSERT INTO 'food' (SELECT null as id, food_id, 'oz' as weight FROM `food` WHERE `weight` NOT LIKE '%oz%');

 

Or if you are wanting to modify those records, and not insert new ones, then

 

UPDATE food SET weight = 'oz' WHERE `weight` NOT LIKE '%oz%');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.