【问题标题】:getting specific columns from three tables从三个表中获取特定列
【发布时间】:2017-12-22 17:35:13
【问题描述】:

我需要通过joins从三个表中获取特定的列。每次出错。我的代码是

    $saf=mysqli_query($db , "select pod.mobile, tpaidamount.Aptdate, follow.cent, pdate, time from pod,tpaidamount, follow where tpaidamount.pid = follow.pid and pod.Customer Id = tpaidamount.pid and pod.Customer Id =follow.pid ");
$i=1;

while($sfg=mysqli_fetch_assoc($saf) or die(mysqli_error($db)))
;
?>

pod,tpaidamount,follow are tables and other coloumns

出现错误 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“Id = tpaidamount.pid 和 pod.Customer Id =follow.pid”附近使用正确的语法

【问题讨论】:

  • 你在“pod.Customer Id =”中有一个空格,去掉Customer和Id之间的空格
  • 在表格中是这样的
  • 如果空格在列名中,请在客户 ID 周围使用反引号。

标签: php mysql database mysqli phpmyadmin


【解决方案1】:

是错字吗?是setfollowup.pid还是follow.pid?

select pod.mobile, tpaidamount.Aptdate, follow.cent, <table>.pdate, <table>.time
from pod, tpaidamount, follow
where tpaidamount.pid = follow.pid 
and pod.`Customer Id` = tpaidamount.pid 
and pod.`Customer Id` = follow.pid

【讨论】:

  • 跟我来看看你的解决方案
  • 嗨,这可能是因为客户和 ID 之间的空间。尝试使用 `` 作为客户 ID。我已经更新了我的建议答案。
【解决方案2】:

您不应该使用空格创建列名。名称:“pod.Customer Id”是一个错误的属性名称,您需要避免使用数据类型(或任何 SGBD 保留字)之类的名称,例如:'date'、'time'、'char'、'table'、'柱子'.... 但是,如果您需要这样做,请尝试以下 SQL:

SELECT p.mobile
, t.Aptdate
, f.cent
, ???.pdate
, ???.`time`
FROM pod AS p
JOIN tpaidamount AS t ON o.`Customer Id` = t.pid 
JOIN follow AS f ON t.pid = f.pid ON p.`Customer Id` = f.pid
  • 使用别名来轻松编码 SQL 查询。例如:table_name AS tn,table_a_name AS tan。

  • !!!我建议您观看一些基本的 SQL 课程。 祝你好运。

【讨论】:

  • 感谢您对属性的建议。我全部更改了。
猜你喜欢
  • 2011-01-06
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多