PHP MYSQL从2个以上的表中检索数据

时间:2015-12-06 11:35:24

标签: php mysql

这是一个查询,它从数据库中获取分页文章列表以及每个文章的“心”数:

$MySQL = '
SELECT
    SQL_CALC_FOUND_ROWS
a.id, a.address, a.autor, a.date_created, a.time_created, a.date_edited,     a.time_edited, a.title, a.content, a.category, a.reads, a.article_type,  a.article_img, a.article_video, COUNT(*) as \'hcount\', ah.ip
FROM articles AS a
LEFT JOIN article_hearts AS ah ON ah.article_id = a.id
GROUP BY a.id, a.address, a.autor, a.date_created, a.time_created, a.title, a.content, a.category, a.reads
ORDER BY a.date_created DESC, a.time_created DESC
LIMIT
    ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . '
';

表:文章

id | address | autor | date_created | time_created | date_edited | time_edited | title content

表:article_hearts

id | ip | article_id

结果是包含所有文章的输出以及每个文章的心数为article_hearts.article_id = articles.id

问题是当我尝试从其他表中获取数据时。

我想从

获得一些评论

表:article_comments

id | id_post | name | etc....

它应该为每篇文章显示它有多少评论....

我使用这个脚本(只是它的一部分......)

// fetch the total number of records in the table
$rows = mysql_fetch_assoc(mysql_query('SELECT FOUND_ROWS() AS rows'));

<?php while ($row = mysql_fetch_assoc($result)):?>

    <?php
    $id = $row['id'];
    $address = $row['address'];
    $autor =  $row['autor'];
    $title =  $row['title'];
     //etc.....

     Echo "......all variables in html....";

       ?>

    php endwhile?>

我尝试了LEFT JOIN,但它不起作用......

我也试过这个:

 $MySQL = '
 SELECT
    SQL_CALC_FOUND_ROWS
  a.id, a.address, a.autor, a.date_created, a.time_created, a.date_edited,  a.time_edited, a.title, a.content, a.category, a.reads, a.article_type,   a.article_img, a.article_video, COUNT(*) as \'hcount\',
   ah.ip, (SELECT * FROM article_comments WHERE id_post=a.id) AS q1
  FROM articles AS a
  LEFT JOIN article_hearts AS ah ON ah.article_id = a.id
 GROUP BY a.id, a.address, a.autor, a.date_created, a.time_created, a.title,     a.content, a.category, a.reads
  ORDER BY a.date_created DESC, a.time_created DESC
  LIMIT
    ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' .   $records_per_page . '
   ';

有什么想法吗?

我的好奇心...... 1.如何检索ac.name =“Guest”

的每篇文章的评论数量
  1. 是否可以从名为article_visits的第4个表中获取计数行 表:article_visits

    id | post_id | ip |等....

  2. 而且,与评论案例一样......获取女巫的价值是显示ip =“YYY ...”中有多少,或者只是检查ip =“YYY”是否在表无论发生多少次

  3. 如果您帮助我提供一些有价值的建议,这将是我理解mysql查询的重要一步。

2 个答案:

答案 0 :(得分:3)

我认为不是加入你可以只做sub select for count

Select *, 
       (Select count(1) from article_comment as ac where ac.article_id = a.id and ac.name = 'Guest'),
       (Select count(1) from article_visits as av where av.article_id = a.id  and up = 'xxxx')
From  article as a 
Group by a.id

答案 1 :(得分:2)

您可以在查询中添加额外的JOIN子句:

SELECT A.id, C.id, H.id
FROM acrticles A
LEFT JOIN comments C ON (A.id=C.articleid_id)
LEFT JOIN hearts H ON (A.id=H.article_id);

聚合函数也在那里工作:

SELECT A.id, COUNT(C.id), H.id
FROM acrticles A
LEFT JOIN comments C ON (A.id=C.articleid_id)
LEFT JOIN hearts H ON (A.id=H.article_id)
GROUP BY (A.id)