I am just beginning, the following queries work but they seem to long and messy. How can I make these queries shorter?
Here is my code:
if ($result = $mysqli->query("select bestelling.datum,klant.klant_code,klant.adres,klant.naam,reis.bestemming,reis.klasse,reis.prijs_in_euro,reis.geannuleerd from klant,reis,bestelling where bestelling.bestelling_code = klant.klant_code and klant.klant_code = reis.reis_code"))
if ($stmt = $mysqli->prepare("UPDATE bestelling,klant,reis SET datum = ?, naam = ?, adres = ?, bestemming = ?, klasse = ?, prijs_in_euro = ?
WHERE klant.klant_code=? and bestelling.bestelling_code = klant.klant_code and klant.klant_code = reis.reis_code"))
if($stmt = $mysqli->prepare("SELECT * FROM bestelling,klant,reis WHERE klant.klant_code=? and bestelling.bestelling_code = klant.klant_code and klant.klant_code = reis.reis_code"))
shorter could be not but more clear yes .. you should not use ancient join based on implicit where condition you should use explicit inner join
if ($result = $mysqli->query("
select bestelling.datum
,klant.klant_code
,klant.adres
,klant.naam
,reis.bestemming
,reis.klasse
,reis.prijs_in_euro
,reis.geannuleerd
from klant
INNER JOIN reis ON klant.klant_code = reis.reis_code
INNER JOIN bestelling ON bestelling.bestelling_code = klant.klant_code
"))
and you could use alias of table name
if ($result = $mysqli->query("
select b.datum
,k.klant_code
,k.adres
,k.naam
,r.bestemming
,r.klasse
,r.prijs_in_euro
,r.geannuleerd
from klant k
INNER JOIN reis r ON k.klant_code = r.reis_code
INNER JOIN bestelling b ON b.bestelling_code = k.klant_code
"))
I recommend to use VIEW
VIEW
is joined table. it makes your code so simple.
try this.
reference: https://www.w3schools.com/sql/sql_view.asp
create view all_info as select bestelling.datum,klant.klant_code,klant.adres,klant.naam,reis.bestemming,reis.klasse,reis.prijs_in_euro,reis.geannuleerd from klant,reis,bestelling where bestelling.bestelling_code = klant.klant_code and klant.klant_code = reis.reis_code
// same upper sql query.
select * from all_info;