piątek, 27 grudnia 2013

Licznik elementów w podkategorii SQL


Mamy następujący problem:
Chcemy ustawić w tabeli licznik podkategorii albo produktów przypisanych do tej kategorii (aby nie wykonwywać tego zapytania cały czas przy wczytywaniu - tylko raz na jakiś czas, albo jednorazowo)

tabelka w bazie danych wygląda tak:

Kategoria:
-id
-nazwa
 -ilosc <-chcemy zliczyć ilość produktów przypisanych do danej kategorii

Produkt:
-id
-nazwa
-kategoria_id <- takie samo jak id kategorii

Aby ustawić ilość w danej kategorii wykonujemy zapytanie:

UPDATE Kategoria SET ilosc = licznik
FROM Kategoria
INNER JOIN
(SELECT COUNT(*) AS licznik FROM Produkt WHERE Kategoria.id=Produkt.kategoria_id ) Produkt ON Kategoria.id = Produkt.kategoria_id

Duplikaty w bazie danych MySQL

Jak pozbyć się duplikatów lub jak wyświetlić duplikaty pól w bazie danych.

SELECT
    name, email, COUNT(*)
FROM
    users
GROUP BY
    name, email
HAVING
    COUNT(*) > 1

inne użycie bardziej zaawansowane:

SELECT *
FROM table
WHERE table.fieldname IN (
 SELECT table.fieldname
 FROM table
 GROUP BY table.fieldname
 HAVING (COUNT(table.fieldname ) > 1)
)

istotną rolę odgrywa tutaj tutaj linijka

having count(*) > 1 dzięki której można wyświetlić rekordy które powtarzają się więcej razy niż ilość określona przez nas.

having count(*) = 1 wyświetlimy rekordy które się nie dublują

SMTP reply codes

Przydatne kody odpowiedzi serwera smtp.

// Some common SMTP status codes are as follows:
// 211 - System status message.
// 214 - Help message formatted for human reader follows.
// 220 - SMTP service ready.
// 221 - Service/connection closing.
// 250 - Successful request. Action completed.
// 251 - Recipient is not local to the server, but the server will accept and forward the message.
// 252 - Recipient cant be verified, but the server will accept the message and attempt delivery.
// 354 - Start message input now, end with .. Indicates the server is ready to accept a message once youve given it From: and To: information
// 421 - Service is not available and connection will be closed.
// 450 - Requested command failed because the recipients mailbox is unavailable.
// 451 - Command has been aborted due to a server error. Possibly notify your SysAdmin.
// 452 - Command has been aborted because the server has insufficient system storage.
// 500 - Server could not recognize the command was due to a syntax error. (usually due to mail client error)
// 501 - Syntax error was found in command arguments. (usually due to mail client error)
// 502 - Command was not implemented. (usually due to mail client error)
// 503 - Server has encounterd a bad command or sequence of commands. (usually due to mail client error)
// 504 - Command parameter is not implemented. (usually due to mail client error)
// 550 - Command failed because the users mailbox was unavailable (or you did not have permissions to send to this mailbox)
// 551 - Recipient is not local to the server. Server responds with a fowarding address that should be tried.
// 552 - Action was aborted because storage allocation was exceeded.
// 553 - Action was aborted because the mailbox name was invalid.
// 554 - Transaction failed, without a clear reason.