我才刚刚开始,以下查询有效,但它们看起来又长又混乱。 如何使这些查询更短?
这是我的代码:
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"))
答案 0 :(得分:2)
更短的可能不是,但更清楚的是。 您应该使用显式内部联接
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
"))
,您可以使用表名的别名
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
"))
答案 1 :(得分:1)
我建议使用VIEW
VIEW
已加入表。它使您的代码变得如此简单。
尝试一下。
参考: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;