Jump to content

how to delete from a table


shaddf
Go to solution Solved by Barand,

Recommended Posts

i have a table with these fields:

table:_projecttbl

fields(P_id,PrT_id,Other_id)

 

I have a string like so: 'HG||Fxg|||ergx||xx'

at every '|||' marks the begining of  PrT_id and Other_id respectively. and it can have more than 64 records.

I would like to write a query  to delete from table where not exists in the string and P_id="known value"

DELETE FROM _projecttbl WHERE NOT EXISTS (SELECT *
FROM _projecttbl AS T1
WHERE T1.Project_id='1');

that is what i have tried but removes all.

Link to comment
Share on other sites

While you are still working with those silly double-delimited strings of yours, forget about using SQL.

 

Break you string into separate records (Prt_id, Oth_id) and store in a separate db table. Then you can do your deletions by matching the tables.

thanks for the idea.Ithought there could be a way of working with the strings direct.I thought  it would be faster

Link to comment
Share on other sites

This is the "real" answer.

could you please verify "real". Is it because that is the way it has been done and will  always be?Is it faster query ?Is it best for sql optimization and server overload? why do you really recommend it.I know it is feasible  option but why?

Link to comment
Share on other sites

The reason why is you NEVER store more than one value in a column. It is beyond a recommendation or an opinion, it is the way it needs to be done. PERIOD.

 

Kinda like you drive cars from the front seat, not the back seat. Its just the way its done.

 

Also, study up on database normalization.

Link to comment
Share on other sites

The reason why is you NEVER store more than one value in a column. It is beyond a recommendation or an opinion, it is the way it needs to be done. PERIOD.

 

Kinda like you drive cars from the front seat, not the back seat. Its just the way its done.

 

Also, study up on database normalization

I chose that because it works for my scenario right now and I got the feel of how to do it using strings instead of going to use temporary tables

Link to comment
Share on other sites

Just for completeness, an FYI to illustrate the SQL solution mentioned earlier

 

The data

mysql> select P_id, Prt_id, Other_id FROM _projecttbl;
+------+--------+----------+
| P_id | Prt_id | Other_id |
+------+--------+----------+
|    1 | AB     | Ghi      |
|    1 | HG     | Fxg      |
|    1 | CD     | Klm      |
|    2 | qwer   | zz       |
|    2 | asdf   | yy       |
|    2 | ergx   | xx       |
+------+--------+----------+

mysql> select Prt_id, Other_id FROM tempproj;
+--------+----------+
| Prt_id | Other_id |
+--------+----------+
| AB     | Ghi      |
| ergx   | xx       |
+--------+----------+

The delete query

DELETE p
FROM _projecttbl p
LEFT JOIN tempproj t USING (Prt_id, Other_id)
WHERE p.P_id = 1 AND t.Prt_id IS NULL;

The result

mysql> select P_id, Prt_id, Other_id FROM _projecttbl;
+------+--------+----------+
| P_id | Prt_id | Other_id |
+------+--------+----------+
|    1 | AB     | Ghi      |
|    2 | qwer   | zz       |
|    2 | asdf   | yy       |
|    2 | ergx   | xx       |
+------+--------+----------+
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.