尝试将我自己的错误修复为现有的代码而不是由我修复。除非相应的id在第二个表中的项目在输出中根本不会显示,否则在第一个表(邮件)中我有多个条目并且它们被过滤掉输出确实显示正确的邮件总数...除非邮件在第二个表中有项目,否则不会输出它。 我尝试使用diff join
进行另一个查询SELECT
id,
messageType,
sender,
mail.receiver,
subject,
body,
has_items,
money,
cod,
checked
FROM
mail
LEFT OUTER JOIN mail_items
ON id = mail_id
LEFT JOIN item_instance
ON item_guid = guid
问题是......我得到错误: 您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以便在“LIMIT 1”附近使用正确的语法
我可以做些什么来使邮件表和mail_items表在从第3个表正确获取项目guid时加入,并输出邮件表条目,即使mail_items表中没有该邮件的项目。
//==========================$_GET and SECURE=================================
$start = (isset($_GET['start'])) ? $sqlc->quote_smart($_GET['start']) : 0;
if (is_numeric($start));
else
$start = 0;
$order_by = (isset($_GET['order_by'])) ? $sqlc->quote_smart($_GET['order_by']) : 'id';
if (preg_match('/^[_[:lower:]]{1,12}$/', $order_by));
else
$order_by = 'id';
$dir = (isset($_GET['dir'])) ? $sqlc->quote_smart($_GET['dir']) : 1;
if (preg_match('/^[01]{1}$/', $dir));
else
$dir = 1;
$order_dir = ($dir) ? 'ASC' : 'DESC';
$dir = ($dir) ? 0 : 1;
//==========================$_GET and SECURE end=============================
$query = $sql->query("SELECT a.id, a.messageType, a.sender, a.receiver, a.subject, a.body, a.has_items, a.money, a.cod, a.checked, b.item_guid, c.itemEntry
FROM mail a INNER JOIN mail_items b ON a.id = b.mail_id LEFT JOIN item_instance c ON b.item_guid = c.guid ORDER BY $order_by $order_dir LIMIT $start, $itemperpage");
$total_found = $sql->num_rows($query);
$this_page = $sql->num_rows($query);
$query_1 = $sql->query("SELECT count(*) FROM `mail`");
$all_record = $sql->result($query_1,0);
答案 0 :(得分:0)
尝试这样的事情:
SELECT
/*
The columns that you want to select
*/
FROM
mail a
LEFT OUTER JOIN mail_items b ON a.id = b.mail_id
LEFT JOIN item_instance c ON b.item_guid = c.guid
注意,当您尝试选择列时,请确保将正确的表别名附加到列名称。这里的表mail
,mail_items
和item_instance
分别以a
,b
,c
作为别名。
如果您使用上述查询为表保留相同的别名,则需要对代码执行以下修改:
$order_by = (isset($_GET['order_by'])) ? $sqlc->quote_smart($_GET['order_by']) : 'id';
if (preg_match('/^[_[:lower:]]{1,12}$/', $order_by));
else
$order_by = 'a.id';
答案 1 :(得分:0)
我不是SQL大师,但我认为
FROM mail a INNER JOIN mail_items b
需要AS
关键字进行别名分配:
FROM mail AS a INNER JOIN mail_items AS b
答案 2 :(得分:0)
尝试这种类型, 从table1选择*作为T1连接table2为T2上的T.id = T2.id将table3连接为T3上的T2.id = T3.id