经过多次提示后,我尝试将以下两个查询结合起来。我认为代码是自我解释的。
表:
评论:id(INT),用户名(VARCHAR),autorpost(TEXT),评论(TEXT), id_post(INT),时间(TIME)
帖子:id(INT),autorid(INT),autor(TEXT), 日期(DATE),经度(FLOAT),纬度(FLOAT),标题(TEXT), 文本(TEXT),城镇(TEXT),时间(TIME)
$hostname='localhost';
$user='root';
$password='';
$useron = $_COOKIE['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=searchfood",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id_post
FROM comments
WHERE username = '$useron'
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($own = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$ownco = $own->fetchAll(PDO::FETCH_COLUMN, 0);
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
try {
$dbh = new PDO("mysql:host=$hostname;dbname=searchfood",$user,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id, autorid, autor, date, longitude, latitude, title, text, town, time
FROM posts
WHERE id in (" . implode(",",$ownco) . ")
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($resco = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$resultcom = $resco->fetchAll();
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
我自己尝试:
"SELECT posts.id, posts.autorid, posts.autor, posts.date, posts.longitude, posts.latitude, posts.title, posts.text, posts.town, posts.time, comments.id_post
FROM posts
INNER JOIN
comments
WHERE id = post_id
ORDER BY id DESC"
答案 0 :(得分:2)
尝试此查询
"SELECT p.*, c.id_post
FROM posts p
JOIN comments c ON( c.id_post = p.id)
ORDER BY p.id DESC"