English 中文(简体)
我怎么能够利用MySQL与一个LEFT JOIN的COUNT?
原标题:How can I use MySQL to COUNT with a LEFT JOIN?
  • 时间:2012-01-11 16:34:44
  •  标签:
  • mysql
  • count

我如何利用MySQL来计算一个LEFT JOIN?

我有两张表格,有时评级表没有照片的评级,因此我认为需要LEFT JOIN,但我还有一份COUNT声明。

<>光线>

id  name        src
1   car         bmw.jpg
2   bike        baracuda.jpg

<>Loves (picid is foreign key withphos id)

id  picid   ratersip
4   1       81.0.0.0
6   1       84.0.0.0
7   2       81.0.0.0

这里,用户只能把自己的图像与知识产权相加。

我想按照最高评级合并两个表格。 新表格

<><>Combined

id  name    src            picid
1   car     bmw.jpg        1
2   bike    baracuda.jpg   2

(bmw)

MySQL代码:

SELECT * FROM photos 
LEFT JOIN ON photos.id=loves.picid 
ORDER BY COUNT (picid);

www.un.org/Depts/DGACM/index_spanish.htm 我的《公共卫生法》:。

$sqlcount = "SELECT p . *
FROM `pics` p
LEFT JOIN (

SELECT `loves`.`picid`, count( 1 ) AS piccount
FROM `loves`
GROUP BY `loves`.`picid`
)l ON p.`id` = l.`picid`
ORDER BY coalesce( l.piccount, 0 ) DESC";

$pics = mysql_query($sqlcount);
最佳回答

MySQL允许你通过id进行分类。 页: 1

select
    p.*
from
    photos p 
    left join loves l on
        p.id = l.picid
group by
    p.id
order by
    count(l.picid)

尽管如此,我知道MySQL在group by 上确实是坏的,因此,你可以尝试将loves在座标上列入一个子座,以便优化:

select
    p.*
from
    photos p
    left join (select picid, count(1) as piccount from loves group by picid) l on
        p.id = l.picid
order by
    coalesce(l.piccount, 0)

我没有我的SQL试验,可以更快地进行测试,这样可以检验。

问题回答

You need to use subqueries:

SELECT id, name, src FROM (
  SELECT photos.id, photos.name, photos.src, count(*) as the_count 
  FROM photos 
  LEFT JOIN ON photos.id=loves.picid 
  GROUP BY photos.id
) t 
ORDER BY the_count
select
      p.ID,
      p.name,
      p.src,
      PreSum.LoveCount
   from
      Photos p
         left join ( select L.picid,
                            count(*) as LoveCount
                        from
                           Loves L
                        group by
                           L.PicID ) PreSum
           on p.id = PreSum.PicID
   order by
      PreSum.LoveCount DESC

I believe you just need to join the data and do a count(*) in your select. Make sure you specify which table you want to use for ambigous columns. Also, don t forget to use a group by function when you do a count(*). Here is an example query that I run on MS SQL.

Select CmsAgentInfo.LOGID, LOGNAME, hCmsAgent.SOURCEID, count(*) as COUNT from hCmsAgent
LEFT JOIN CmsAgentInfo on hCmsAgent.logid=CmsAgentInfo.logid
where SPLIT =  990 
GROUP BY CmsAgentInfo.LOGID, LOGNAME, hCmsAgent.SOURCEID

成果就是这样。

77615   SMITH, JANE 1   36
29422   DOE, JOHN   1   648

Hope that helps. Good Luck.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签