Jump to content

how to delete from a table


shaddf

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
https://forums.phpfreaks.com/topic/298484-how-to-delete-from-a-table/
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.

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

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')";

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.

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?

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.

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

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       |
+------+--------+----------+

Archived

This topic is now archived and is closed to further replies.



×
×
  • 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.