MSSQL - tips for the mySQL user

Pentru utilizatorii de mySQL e greu cand se vad pe o lpatforma MSSQL. Iata 2 chestii folositoare, care ne enerveaza la MSSQL (si nu numai), pentru ca in alte motoare de baze de date nu le avem :

In MSSQL nu avem LIMIT, nu avem LIMIT, cum procedam ? Iata o sugestie :

select * from (
   select top 10 user_id,user_name from (
  select top 30 user_id,user_name from employee order by user_name asc
  ) as table1 order by user_name desc
) as table2 order by user_name asc

O alta chestie care se comporta diferit este clauza GROUP BY.

In mySQL putem face chestii de genul (nu foarte intelepte, dar totusi utile uneori, si vazute destul de des in aplicatii) :

Select * FROM products
INNER JOIN properties on properties.product_id = products.product_id
WHERE status_id = 1
GROUP BY product_id

In MSSQL soate campurile din select trebuie sa fie agregate sau in clauza pentru a putea face un group by (suna ca dracu in romana).

asa ca query-ul se sparge in 2 si vom folosi operatorul IN (query) :

SELECT * FROM products
INNER JOIN properties on properties.product_id = products.product_id
where status_id IN (
 SELECT product_id FROM products
 WHERE status_id = 1
 GROUP BY product_id
)

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.