Toon posts:

[mysql] joins gebruiken in delete-clausule

Pagina: 1
Acties:

Verwijderd

Topicstarter
Is dit wel mogelijk? Want bij de onderstaande code krijg ik een Unknown table 'contests_contest_scores' in where clause.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
<?php

$sQuery = "DELETE FROM
                contest_scores
           WHERE
                contests_contest_scores.contest_id = 1
           AND
                contests_contest_scores.contest_score_id = contest_scores.contest_score_id";                
                        
mysql_query($sQuery);

?>

[ Voor 6% gewijzigd door Verwijderd op 09-05-2004 18:27 ]


  • NMe
  • Registratie: Februari 2004
  • Laatst online: 24-05 14:53

NMe

Quia Ego Sic Dico.

Volgens mij moet je daar HAVING gebruiken ipv WHERE. Maar het kan ook best wel eens zo zijn dat het niet mogelijk is om naar andere tabellen te kijken bij een delete... Weet het niet zeker.

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


  • Michali
  • Registratie: Juli 2002
  • Laatst online: 22-03 18:12
Ik denk dat het zo moet:
code:
1
2
3
4
5
6
7
8
9
DELETE
  tabel1
FROM
  tabel1,
  tabel2
WHERE
  tabel2.id = 1
AND
  tabel1.tabel2_id = tabel2.id

Dan moet dat lukken. Verder kun je ook nog hier kijken.

edit: .* weggehaald (optioneel)

Het kan ook zo:
code:
1
2
3
4
5
6
7
8
9
DELETE FROM
  tabel1
USING
  tabel1,
  tabel2
WHERE
  tabel2.id = 1
AND
  tabel1.tabel2_id = tabel2.id

[ Voor 27% gewijzigd door Michali op 09-05-2004 18:37 ]

Noushka's Magnificent Dream | Unity


  • NMe
  • Registratie: Februari 2004
  • Laatst online: 24-05 14:53

NMe

Quia Ego Sic Dico.

Michali schreef op 09 mei 2004 @ 18:33:
Ik denk dat het zo moet:
code:
1
2
3
4
5
6
7
8
9
DELETE
  tabel1
FROM
  tabel1,
  tabel2
WHERE
  tabel2.id = 1
AND
  tabel1.tabel2_id = tabel2.id

Dan moet dat lukken. Verder kun je ook nog hier kijken.

edit: .* weggehaald (optioneel)

Het kan ook zo:
code:
1
2
3
4
5
6
7
8
9
DELETE FROM
  tabel1
USING
  tabel1,
  tabel2
WHERE
  tabel2.id = 1
AND
  tabel1.tabel2_id = tabel2.id
Ben je niet bang dattie met die eerste oplossing uit beide tabellen records gaat verwijderen? Als ik die query zo zie, dan zou ik zeggen van wel namelijk, daarom heb ik die oplossing hier ook niet gepost. :)

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


  • whoami
  • Registratie: December 2000
  • Laatst online: 25-05 23:56
Een HAVING clausule heeft ie alleszins niet nodig, en een delete statement kan slechts uit 1 tabel records verwijderen.

Je kan een DELETE statement zo forumleren:
code:
1
2
3
4
DELETE tabel
FROM tabel1
WHERE tabel.id = tabel1.fk_id
WHERE tabel1.naam = 'blaat'

Deze query gaat enkel records uit de tabel 'tabel' verwijderen,

bovenstaande syntax is gebaseerd op T-SQL (MS SQL Server), dus ik weet niet precies hoe het in MySQL zit; ik zie die using clause hierboven, maar die ken ik niet.

[ Voor 24% gewijzigd door whoami op 09-05-2004 21:23 ]

https://fgheysels.github.io/


  • Michali
  • Registratie: Juli 2002
  • Laatst online: 22-03 18:12
NMe84 schreef op 09 mei 2004 @ 21:08:
[...]

Ben je niet bang dattie met die eerste oplossing uit beide tabellen records gaat verwijderen? Als ik die query zo zie, dan zou ik zeggen van wel namelijk, daarom heb ik die oplossing hier ook niet gepost. :)
Mysql Manual - Delete Syntax

For the first syntax, only matching rows from the tables listed before the FROM clause are deleted. For the second syntax, only matching rows from the tables listed in the FROM clause (before the USING clause) are deleted. The effect is that you can delete rows from many tables at the same time and also have additional tables that are used for searching:

DELETE t1,t2 FROM t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id;

Or:

DELETE FROM t1,t2 USING t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id;

These statements use all three files when searching for rows to delete, but delete matching rows only from tables t1 and t2.
Het staat er duidelijk

[ Voor 4% gewijzigd door Michali op 09-05-2004 21:28 ]

Noushka's Magnificent Dream | Unity


  • NMe
  • Registratie: Februari 2004
  • Laatst online: 24-05 14:53

NMe

Quia Ego Sic Dico.

Mjah, idd, weer wat geleerd. :) En TS ook hoop ik.

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Verwijderd

Topicstarter
'Using' werkte i.d.d. perfect! Ik weet niet of het nodig is om steeds een soort van: 'bedankt voor de oplossing, het werkt' te moeten posten, aangezien groeten e.d. ook niet gewaardeerd wordt?

  • Michali
  • Registratie: Juli 2002
  • Laatst online: 22-03 18:12
Verwijderd schreef op 10 mei 2004 @ 13:08:
'Using' werkte i.d.d. perfect! Ik weet niet of het nodig is om steeds een soort van: 'bedankt voor de oplossing, het werkt' te moeten posten, aangezien groeten e.d. ook niet gewaardeerd wordt?
Het is wel fijn even aan te geven of een van de gegeven oplossing het probleem verhelpt. Dan kun je ook gelijk aangeven welke oplossing het probleem heeft verholpen zodat mensen die later met een soortgelijk probleem hier gaan zoeken en dit topic vinden, ook gelijk zeker weten dat er een werkende oplossing is. En uiteraard vinden sommige mensen het ook wel fijn als ze weten dat ze iemand hebben kunnen helpen.

Noushka's Magnificent Dream | Unity

Pagina: 1