dongyao2022 2014-06-04 16:53
浏览 35
已采纳

如何从两个表中连接两个mysql查询

I have two independents querys:

Query 1 (This query get all gallery from my table) :

$showgallerys = mysqli_query($con,"SELECT * FROM canais");
while($row = mysqli_fetch_array($showgallerys)) {
echo '<div class="canal-nome">'.$row['nome'].'</div>;

Query 2 (This query count numbers of photos per category):

$q="SELECT categoria, COUNT(titulo) FROM galerias GROUP BY categoria ";
$res=mysqli_query($con,$q);
while($row = mysqli_fetch_array($res)){
 echo '
('. $row['COUNT(titulo)'] .')';
}

I need to show the name of the gallery (query 1) with the numbers of photos (query 2)

like this Gallery name (30)

This the structure of first table (called canais):

  id | nome            | htd     | imagem   | thumb    |
   1 | Gallery Nature  | nature  | face.jpg | thumb.jg | 
   2 | Gallery Peoples | people  | face.jpg | thumb.jg | 
   3 | Gallery Animals | animal  | face.jpg | thumb.jg | 

This the structure of second table (called galerias)

  id | titulo       | foto    | thumb           | data    | categoria |
  1  | Sun          | sun.jpg | sun-thumb.jpg   | now     | nature    |
  2  | Moon         | mon.jpg | mon-thumb.jpg   | now     | nature    |
  3  | Tree         | tree.jpg| tre-thumb.jpg   | now     | nature    |
  4  | Woman        | wman.jpg| wman-thumb.jpg  | now     | people    |
  5  | Girl         | gran.jpg| gr-thumb.jpg    | now     | people    |
  6  | leaf         | lea.jpg | leaf-thumb.jpg  | now     | nature    |
  7  | dog          | dog.jpg | dog-thumb.jpg   | now     | animal    |

In this case i need show the results like this:

   Gallery name --> Gallery Nature (4) <-- Number of  occurrences 
   Gallery name --> Gallery People (2) <-- Number of  occurrences 
   Gallery name --> Gallery Animal (1) <-- Number of  occurrences 

being the name of the gallery must be obtained through the "canais" table and the number of occurrences must come from the "galerias" table based in the "categoria" column. Can anyone help me to solve it ?

  • 写回答

1条回答 默认 最新

  • dongya767979565 2014-06-04 16:57
    关注

    Below query will help you

    SELECT a.nome AS gallery  ,COUNT(b.titulo)  AS photos 
    FROM canais a INNER JOIN galerias b ON a.htd    = b.categoria 
    GROUP BY a.nome
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目标计数模型训练过程中的问题
  • ¥100 Acess连接SQL 数据库后 不能用中文筛选
  • ¥15 用友U9Cloud的webapi
  • ¥20 电脑拓展屏桌面被莫名遮挡
  • ¥20 ensp,用局域网解决
  • ¥15 Python语言实验
  • ¥15 我每周要在投影仪优酷上自动连续播放112场电影,我每一周遥控操作一次投影仪,并使得电影永远不重复播放,请问怎样操作好呢?有那么多电影看吗?
  • ¥20 电脑重启停留在grub界面,引导出错需修复
  • ¥15 matlab透明图叠加
  • ¥50 基于stm32l4系列 使用blunrg-ms的ble gatt 创建 hid 服务失败