Curs Php-mysql

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 atribut face ca optiunea din <select> sa fie selectata?
checked="checked" selected="selected" disabled="disabled"
<select name="a_name">
 <option value="val1">Optiune 1</option>
 <option value="val2" selected="selected">Optiune 2</option>
</select>
Care din aceste valori CSS creaza culoare gradient pentru fundal?
contain repeat-x linear-gradient
#id {
  background: linear-gradient(top left, #1f1, #fff, #11f);
}
Care cod creaza un array in JavaScript?
[] {} new Object()
var arr = [1, "CoursesWeb.net", "MarPlo.net"];
alert(arr[2]);
Indicati functia PHP folosita pentru redirect la alta pagina.
function() header() switch()
header("Location: http://coursesweb.net/");
exit;
Indicati articolul corect la cuvantul "garden" in propozitia: "... garden is a place with flowers".
An The A
The garden is a place with flowers.
- Gradina este un loc cu flori.
Indicati articolul corect pentru cuvantul: "jardín" (gradina)
la los el
El jardín es un lugar de flores.
- Gradina e un loc cu flori.
Select in doua tabele MySQL

Last accessed pages

  1. Jokes - Glume, Bancuri, Humor (2) (17325)
  2. Creare Meniu Dublu (2843)
  3. PHP Functii Anonime - Closures (925)
  4. Curs si Tutoriale JavaScript (134779)
  5. Lectia 89, Recapitulare 77-78 (81)

Popular pages this month

  1. Coduri pt culori (122)
  2. Cursuri si Tutoriale: Engleza, Spaniola, HTML, CSS, Php-Mysql, JavaScript, Ajax (88)
  3. Curs HTML gratuit Tutoriale HTML5 (73)
  4. Elemente principale HTML (40)
  5. Exercitii engleza - English Tests and exercises - Grammar (39)