Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Warunkowe wykonywanie zapytań
Aztech
post 28.11.2006, 09:17:31
Post #1





Grupa: Zarejestrowani
Postów: 276
Pomógł: 3
Dołączył: 22.10.2003
Skąd: Wrocław

Ostrzeżenie: (0%)
-----


Witam, mam sekwencję zamieniającą mi w drzewku dwa poddrzewa miejscami, wykonującą się jako ciąg instrukcji w MySQL.
Chciałbym jednak, by część instrukcji wykonała się warunkowo i nie bardzo mam pomysł jak poprawić moje zapytania
Pierwsza wersja
  1. LOCK TABLE rbx_category WRITE;
  2. SELECT @toMoveLft := lft, @toMoveRgt := rgt,
  3. @toMoveWidth := rgt - lft + 1
  4. FROM rbx_category WHERE category_id = 58;
  5.  
  6. SELECT @toReplaceLft := lft, @toReplaceRgt := rgt,
  7. @toReplaceWidth := rgt - lft + 1
  8. FROM rbx_category WHERE rgt = @toMoveLft - 1;
  9.  
  10. UPDATE rbx_category #1
  11. SET rgt = rgt - @toReplaceWidth
  12. WHERE rgt BETWEEN @toMoveLft AND @toMoveRgt;
  13.  
  14. UPDATE rbx_category #2
  15. SET rgt = rgt + @toMoveWidth
  16. WHERE lft BETWEEN @toReplaceLft AND @toReplaceRgt;
  17.  
  18. UPDATE rbx_category #3
  19. SET lft = lft - @toReplaceWidth
  20. WHERE rgt BETWEEN @toReplaceLft AND @toReplaceLft + @toMoveWidth;
  21.  
  22. UPDATE rbx_category #4
  23. SET lft = lft + @toMoveWidth
  24. WHERE rgt BETWEEN ( @toMoveRgt - @toMoveWidth ) AND @toMoveRgt;
  25. UNLOCK TABLES;

Chciałbym by UPDATE'y #1 - #4 wykonały się tylko w sytuacji gdy @toReplaceWidth będzie większe od zera.
Wymyśliłem więc coś takiego:
  1. LOCK TABLE rbx_category WRITE;
  2. SELECT @toMoveLft := lft, @toMoveRgt := rgt,
  3. @toMoveWidth := rgt - lft + 1
  4. FROM rbx_category WHERE category_id = 58;
  5.  
  6. SELECT @toReplaceLft := lft, @toReplaceRgt := rgt,
  7. @toReplaceWidth := rgt - lft + 1
  8. FROM rbx_category WHERE rgt = @toMoveLft - 1;
  9.  
  10. UPDATE rbx_category #1
  11. SET rgt = rgt - @toReplaceWidth
  12. WHERE rgt BETWEEN @toMoveLft AND @toMoveRgt AND @toReplaceWidth > 0;
  13.  
  14. UPDATE rbx_category #2
  15. SET rgt = rgt + @toMoveWidth
  16. WHERE lft BETWEEN @toReplaceLft AND @toReplaceRgt AND @toReplaceWidth > 0;
  17.  
  18. UPDATE rbx_category #3
  19. SET lft = lft - @toReplaceWidth
  20. WHERE rgt BETWEEN @toReplaceLft AND @toReplaceLft + @toMoveWidth AND @toReplaceWidth > 0;
  21.  
  22. UPDATE rbx_category #4
  23. SET lft = lft + @toMoveWidth
  24. WHERE rgt BETWEEN ( @toMoveRgt - @toMoveWidth ) AND @toMoveRgt AND @toReplaceWidth > 0;
  25. UNLOCK TABLES;

Ale może jest lepszy sposób nić dodawanie wszędzie: AND @toReplaceWidth > 0 questionmark.gif?
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 18.07.2025 - 18:40