In acest tutorial puteti invata
cum se pot selecta coloane din doua tabele MySQL intr-o
singura interogare.
Cand in comanda SQL sunt selectate coloane din tabele diferite, trebuie adaugat numele tabelului inainte de cel al coloanei (despartite prin punct "."). Sintax este:
SELECT `tabel1`.`coloana`, `tabel2`.`coloana` FROM `tabel1`, `tabel2` WHERE conditie
Sa vedem cateva exemple,in care se vor folosi urmatoarele doua tabele, denumite "categories" si "links".
categories
id |
category |
1 |
PHP-MySQL |
2 |
HTML |
links
id |
link |
visits |
1 |
marplo.net/php-mysql/matrice_tablouri.html |
12 |
1 |
marplo.net/php-mysql/siruri.html |
15 |
2 |
marplo.net/html/tabele.html |
18 |
1. Selectare toate coloanele din tabelul "categories" unde
id=2, si coloanele "link" si "visits" unde
visits>13.
SELECT `categories`.*, `links`.`link`, `links`.`visits` FROM `categories`, `links` WHERE `categories`.`id`=2 AND `links`.`visits`>13
Rezultat:
| id | category | link | visits |
------------------------------------------------------------------
| 2 | HTML | marplo.net/php-mysql/siruri.html | 15 |
| 2 | HTML | marplo.net/html/tabele.html | 18 |
2. Selecteaza randurile din coloanele "category" si "link", unde '
id'-ul din tabelul 'categories' are valoarea 1, si
id-ul din tabelul 'links' este egal cu id-ul din 'categories'.
SELECT `categories`.`category`, `links`.`link` FROM `categories`, `links` WHERE `categories`.`id`=1 AND `categories`.`id`=`links`.`id`
Rezultat:
| category | link |
--------------------------------------------------------------
| PHP-MySQL | marplo.net/php-mysql/matrice_tablouri.html |
| PHP-MySQL | marplo.net/php-mysql/siruri.html |
Doua SELECT intr-o interogare
Se poate de asemenea executa doua comenzi SELECT in aceeasi interogare SQL.
Exemple:
1. Selectare randuri din coloana "link" (tabel 'links') unde valoarea coloanei "id" corespunde cu id-ul pentru valoarea
HTML (din tabelul 'categories').
SELECT `link` FROM `links` WHERE `id`=(SELECT `id` FROM `categories` WHERE `category`='HTML')
Rezultat:
| link |
---------------------------------------
| marplo.net/html/tabele.html |
2. Returneaza numarul total de randuri din tabelul 'links', unde
visits>14, si se selecteaza inregistrarile din coloana "category" (tabelul 'categories') unde
id<4.
SELECT (SELECT COUNT(*) FROM `links` WHERE `visits`>14) AS nrl, `category` FROM `categories` WHERE `id`<4
Rezultat:
| nrl | category |
-------------------
| 2 | PHP-MySQL |
| 2 | HTML |
- Exista si o alta modalitate de a selecta coloane din doua tabele diferite, folosind instructiuni JOIN, prezentate in tutorialul
MySQL INNER JOIN, LEFT JOIN, RIGHT JOIN.
Un Test simplu in fiecare zi
HTML
CSS
JavaScript
PHP-MySQL
Engleza
Spaniola
Care tag HTML5 defineste un text marcat, evidentiat?
<mark> <embed> <span><p>Cursuri graruite: <mark>MarPlo.net</mark> , jocuri, anime.</p>
Ce pseudo-clasa CSS defineste un stil la element cand mouse-ul e deasupra lui?
:focus :hover :activea:hover {
font-weight: bold;
color: #00da01;
}
Clic pe functia ce returneaza un sir cu un numar rotunjit la x decimale.
toPrecision(x) toFixed(x) floor(x)var num = 12.34567;
num = num.toFixed(2);
alert(num); // 12.35
Indicati functia PHP care adauga continutul unui fisier intr-un array.
[) file() readfile()$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
var_export($arr);
Clic pe raspunsul potrivit la intrebarea: "What time is it?"
On the 7th of July 1996 It is a quarter to 5. Nice weather.What time is it? It is a quarter to 5.
- Cat este ora? E 5 fara un sfert.
Indicati raspunsul potrivit la intrebarea: "¿Qué hora es?"
Hace buen tiempo. En el 7 de julio de 1996 Son las tres menos cuarto.¿Qué hora es? Son las tres menos cuarto.
- Cat este ora? E 3 fara un sfert.