Website Development Prices

Search Blog

Monday, October 12, 2015

Osnovni podupiti (Basic subqueries)

Podupiti se najcesce koriste kada se rezultat jednog (unutrasnjeg) upita poredi u uslovu drugog (spoljasnjeg) upita. 

Ako zelite da pronadjete porudzbinu sa najvecim iznosom.

Primer:

select sifrakupca, iznos
from porudzbine
where iznos = (select max(iznos) from porudzbine);

Napomena: nema razmak izmedju max i otvorene zagrade.

Ovaj fajl sacuvajte kao osnovni-podupiti.sql.

1. Pokrenite XAMPP. Kliknite na Admin u liniji MySQL-a ili ukucajte u address bar http://localhost/phpmyadmin/

2. Sa leve strane izaberite bazu podataka volimmotore, a sa desne izberite karticu SQL.

3. Ubacite ovaj kod i kliknite dugme "Go".





Objasnjenje:

Podupit vraca samo jednu vrednost (najveci iznos porudzbine), koja se u spoljasnjem upitu koristi za poredjenje,.

Isti rezultat mozete dobiti i upotrebom sledeceg upita.

Primer 2: 

select sifrakupca, iznos
from porudzbine
order by iznos desc

limit 1;

4. Ponovo kliknite na SQL karticu, dodajte ovaj kod i kliknite dugme "Go".



Objasnjenje:

U MySQL-u se efikasnije izvrsava od verzije s podupitom, ali posto sadrzi odredbu limit, ovaj upit nije kompatibilan s vecinom sistema za upravljanje relacionim bazama podataka. 

Vrednosti koje vracaju podupiti mozete koristiti u svim uobicajenim operacijama poredjenja. Postoji i nekoliko specijalnih operatora za podupite.


 operatori za podupite

Svaki od specijalnih operatora moze se navesti samo iza jednog operatora za poredjenje, osim operatora in, u kome je operator za poredjenje (=) u neku ruku "upakovan". 


Subqueries are commonly used when the result of an (inner) query compares the condition of another (the outside) query.

If you want to find order with the largest amount.

Example:

SELECT customerid, amount
FROM orders
WHERE amount = (SELECT max(amount) FROM orders);

Note: there is no space between the max and the opening parenthesis.

Save this file as basic-subqueries.sql.

1. Start XAMPP. Click on the Admin in line MySQL or type in the address bar 
http://localhost/phpmyadmin/

2. From the left select the database lovebikes, and on the right select the SQL tab.

3. Insert this code and click "Go".






Explanation:

The subquery returns only one value (the largest amount of order), which in the outer query is used to compare.

The same result can be obtained by using the following query.

Example 2:

SELECT customerid, amount
FROM orders
ORDER BY amount DESC
LIMIT 1;

4. Click again on SQL tab, add the code, and click "Go".




Explanation:

In MySQL it is efficiently executed from version with subqueries, but because it contains a clause LIMIT, this query is not compatible with most relational databases management systems.

The values that subqueries return can be used in all normal  comparison  operations. There are some special operators for subqueries.



Each of the special operators can be given only after onen operator for comparison, other than the operator IN, where the comparison operator (=) in a sense, "is packaged".

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.