shaddf Posted October 8, 2015 Share Posted October 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2015 Share Posted October 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
shaddf Posted October 8, 2015 Author Share Posted October 8, 2015 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 Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted October 8, 2015 Solution Share Posted October 8, 2015 This could work $str = 'HG||Fxg|||ergx||xx'; // replace "|||" with "','" so you have separate records split by quote-comma-quote $str = str_replace("|||", "','", $str); $sql = "DELETE FROM _projecttbl WHERE P_id = 1 AND CONCAT(Prt_id, '||', Oth_id) NOT IN ('$str')"; Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 8, 2015 Share Posted October 8, 2015 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. This is the "real" answer. Quote Link to comment Share on other sites More sharing options...
shaddf Posted October 8, 2015 Author Share Posted October 8, 2015 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? Quote Link to comment Share on other sites More sharing options...
benanamen Posted October 8, 2015 Share Posted October 8, 2015 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. Quote Link to comment Share on other sites More sharing options...
shaddf Posted October 8, 2015 Author Share Posted October 8, 2015 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted October 8, 2015 Share Posted October 8, 2015 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 | +------+--------+----------+ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.