Postgres INNER JOIN查询问题

时间:2018-08-07 04:17:09

标签: php postgresql

我有两个表user和discovery,但是我无法使用postgres和php中的join查询它们。我的查询是

$query = 'SELECT discovery.id,user.avatar,user.name,user.city,user.country,
discovery.image,discovery.likes,discovery.pincount FROM discovery
WHERE (discovery.id=$1)
INNER JOIN user ON (discovery.user=user.id)';
$res = pg_query_params($query, array($_POST['discovery_id']));

我遇到错误

syntax error at or near "."

我在发现表中有一个现场用户,还有一个名为用户的表。

2 个答案:

答案 0 :(得分:0)

请尝试这个

$query = 'SELECT d.id, u.avatar, u.name, u.city, u.country, 
d.image, d.likes, d.pincount FROM "discovery" AS d
INNER JOIN "user" AS u ON (d.user = u.id) WHERE (d.id = $id)';
$res = pg_query_params($query, array($_POST['discovery_id']));

发生的问题是由于user是postgressql(postgresql.org/docs/8.1/static/sql-keywords-appendix.html)上的保留字,表名user没有{{ 1}}被称为关键字

答案 1 :(得分:0)

尝试此查询

$query = "SELECT discovery.id,user.avatar,user.name,user.city,user.country,
discovery.image,discovery.likes,discovery.pincount FROM discovery d1
INNER JOIN user d2 ON d1.user=d2.id WHERE d1.id='1'";