Databases

NAVIGATION
CATEGORIES
REFERRENCE
LINKS
  • sql counting duplicates

    7 answers - 1487 bytes - related search similar search Add To My Delicious Add To My Stumble Upon Add To My Google Mark Add To My Facebook Add To My Digg Add To My Reddit

    Hy
    I have this code:
    $result = mysql_query("select * from smjestaj left join grad on
    grad.grad=smjestaj.grad_pbroj order by grad_pbroj ASC", $db) or die ("Could
    not read data because ".mysql_error());
    // print the data in a table
    if (mysql_num_rows($result)) {
    print "<table cellspacing=0 border=1 width=\"25%\" class=\"redovi\">\n";
    print "<tr class=\"headline\"><tdGrad </td></tr>";
    while ($qry = mysql_fetch_array($result)) {
    print "<tr><td><a href=\"" .
    "$qry[grad_pbroj]\"
    >$qry[Naziv]</a></td>";

    print "</tr>\n";
    }
    print "</table>\n";
    }
    mysql_close($db);
    Table smjestaj has coloumns:
    id | Name | grad_pbroj |
    Table grad has coloumns:
    grad_pbroj | Naziv|
    Now i wanna display all the grad_pbroj coloumns from 'smjestaj', buth not
    displaying duplicates, just to count them, and put the count behind the
    'grad_pbroj' coloumn.
    Result:
    Naziv_first (3)
    Naziv_second (1)
    Naziv_third (8)
    I nead full solution, because i have tried everythig and either sql of php
    displaying is ****ing me.
    I have tried all of that group by, count sql syntax, and i allways get
    error.
    Anyone cann help me plz.
    Dejan
    ps. sorry to all the people i have asked this before, buth not giving full
    description (Arjen, Jerry Stuckle).
  • No.1 | | 402 bytes | |

    Dejan wrote:
    Now i wanna display all the grad_pbroj coloumns from 'smjestaj', buth not
    displaying duplicates, just to count them, and put the count behind the
    'grad_pbroj' coloumn.

    Result:
    Naziv_first (3)
    Naziv_second (1)
    Naziv_third (8)

    Is this what you want?

    SELECT first_column, ****(*) FRM my_table GRUP BY first_column;

  • No.2 | | 671 bytes | |

    Yeah, i know that, buth i have left joinorder by group by.

    And i have sql syntax error, and don't know how to display related coloumn,
    and number of duplicates.

    Sjoerd <sjoerder@gmail.comwrote in message
    news:1145009057.659424.240850@
    Dejan wrote:
    Now i wanna display all the grad_pbroj coloumns from 'smjestaj', buth
    not
    displaying duplicates, just to count them, and put the count behind the
    'grad_pbroj' coloumn.

    Result:
    Naziv_first (3)
    Naziv_second (1)
    Naziv_third (8)

    Is this what you want?

    SELECT first_column, ****(*) FRM my_table GRUP BY first_column;

  • No.3 | | 1066 bytes | |

    Dejan wrote:
    Yeah, i know that, buth i have left joinorder by group by.

    And i have sql syntax error, and don't know how to display related coloumn,
    and number of duplicates.
    --
    Sjoerd <sjoerder@gmail.comwrote in message
    news:1145009057.659424.240850@
    >
    >>Dejan wrote:
    >>

    Now i wanna display all the grad_pbroj coloumns from 'smjestaj', buth

    not

    displaying duplicates, just to count them, and put the count behind the
    'grad_pbroj' coloumn.

    Result:
    Naziv_first (3)
    Naziv_second (1)
    Naziv_third (8)
    >>
    >>Is this what you want?
    >>
    >>SELECT first_column, ****(*) FRM my_table GRUP BY first_column;
    >>

    >
    >
    >


    Well, post your attempt(s) and maybe we can see what's wrong!

    Sjoerd showed you how to do it. We can't do much more with the information
    you've provided.
  • No.4 | | 1363 bytes | |

    The solution is:

    select grad_pbroj, Naziv, ****(*) as brojac from smjestaj left join grad on
    grad.grad=smjestaj.grad_pbroj group by grad_pbroj order by grad_pbroj ASC

    bye
    Dejan

    Jerry Stuckle <jstucklex@attglobal.netwrote in message
    @comcast.com
    Dejan wrote:
    Yeah, i know that, buth i have left joinorder by group by.

    And i have sql syntax error, and don't know how to display related
    coloumn,
    and number of duplicates.
    --
    Sjoerd <sjoerder@gmail.comwrote in message
    news:1145009057.659424.240850@
    >
    >>Dejan wrote:
    >>

    Now i wanna display all the grad_pbroj coloumns from 'smjestaj', buth

    not

    displaying duplicates, just to count them, and put the count behind the
    'grad_pbroj' coloumn.

    Result:
    Naziv_first (3)
    Naziv_second (1)
    Naziv_third (8)
    >>
    >>Is this what you want?
    >>
    >>SELECT first_column, ****(*) FRM my_table GRUP BY first_column;
    >>

    >
    >
    >
    >

    Well, post your attempt(s) and maybe we can see what's wrong!

    Sjoerd showed you how to do it. We can't do much more with the
    information
    you've provided.
    --
  • No.5 | | 1032 bytes | |


    | The solution is:
    |
    | select grad_pbroj, Naziv, ****(*) as brojac from smjestaj left join grad
    on
    | grad.grad=smjestaj.grad_pbroj group by grad_pbroj order by grad_pbroj ASC

    i don't think sothat should cough up a nasty error as "naziv" is not
    handled in the group by clause nor is it part of an aggrigate functionand
    that's just me eye-balling it for .5 seconds.

    btwpeople who DNT format their inline sql should be shot! which is more
    manageable, the above or:

    SELECT s.grad_pbroj ,
    s.Naziv ,
    ****(*) brojac
    FRM smjestaj s
    LEFT JIN grad g N
    g.grad = s.grad_pbroj
    GRUP BY s.grad_pbroj ,
    s.Naziv
    RDER BY s.grad_pbroj ASC

    ** and if this query is to return the number of duplicates, then there
    should either be a where or having clause that only returns a row if the
    count is 1.

    but that's just me following real-world, professional standards. the former
    should just be all on one line for christ' sake.

  • No.6 | | 1279 bytes | |

    robert wrote:
    | The solution is:
    |
    | select grad_pbroj, Naziv, ****(*) as brojac from smjestaj left join grad
    on
    | grad.grad=smjestaj.grad_pbroj group by grad_pbroj order by grad_pbroj ASC
    --
    i don't think sothat should cough up a nasty error as "naziv" is not
    handled in the group by clause nor is it part of an aggrigate functionand
    that's just me eye-balling it for .5 seconds.

    btwpeople who DNT format their inline sql should be shot! which is more
    manageable, the above or:

    SELECT s.grad_pbroj ,
    s.Naziv ,
    ****(*) brojac
    FRM smjestaj s
    LEFT JIN grad g N
    g.grad = s.grad_pbroj
    GRUP BY s.grad_pbroj ,
    s.Naziv
    RDER BY s.grad_pbroj ASC
    --
    ** and if this query is to return the number of duplicates, then there
    should either be a where or having clause that only returns a row if the
    count is 1.

    but that's just me following real-world, professional standards. the former
    should just be all on one line for christ' sake.
    --

    Close - you would need a HAVING clause:

    SELECT s.grad_pbroj, s.Naziv, ****(*) AS brojac
    FRM smjestaj s
    LEFT JIN grad g N g.grad = s.grad_pbroj
    GRUP BY s.grad_pbroj,s.Naziv
    HAVING ****(*) 1
    RDER BY s.grad_pbroj ASC
  • No.7 | | 2115 bytes | |

    andyou'd want to put the standard inline sql formatting *back in*each
    column named and on a seperate line, delimiting columns aligned, table names
    aliased and aligned, reserved words in caps, conditions aligned (=, !=, IN,
    NT IN, etc.)regardless of select, insert, update, etc.

    and that's not just *my* pet-peave. the more complex the query, view, proc,
    or udf the more the differences are noticed and appreciated.

    "Jerry Stuckle" <jstucklex@attglobal.netwrote in message
    @comcast.com
    | robert wrote:
    | | The solution is:
    | |
    | | select grad_pbroj, Naziv, ****(*) as brojac from smjestaj left join
    grad
    | on
    | | grad.grad=smjestaj.grad_pbroj group by grad_pbroj order by grad_pbroj
    ASC
    | >
    | >
    | i don't think sothat should cough up a nasty error as "naziv" is not
    | handled in the group by clause nor is it part of an aggrigate
    functionand
    | that's just me eye-balling it for .5 seconds.
    | >
    | btwpeople who DNT format their inline sql should be shot! which is
    more
    | manageable, the above or:
    | >
    | SELECT s.grad_pbroj ,
    | s.Naziv ,
    | ****(*) brojac
    | FRM smjestaj s
    | LEFT JIN grad g N
    | g.grad = s.grad_pbroj
    | GRUP BY s.grad_pbroj ,
    | s.Naziv
    | RDER BY s.grad_pbroj ASC
    | >
    | >
    | ** and if this query is to return the number of duplicates, then there
    | should either be a where or having clause that only returns a row if the
    | count is 1.
    | >
    | but that's just me following real-world, professional standards. the
    former
    | should just be all on one line for christ' sake.
    | >
    | >
    |
    | Close - you would need a HAVING clause:
    |
    | SELECT s.grad_pbroj, s.Naziv, ****(*) AS brojac
    | FRM smjestaj s
    | LEFT JIN grad g N g.grad = s.grad_pbroj
    | GRUP BY s.grad_pbroj,s.Naziv
    | HAVING ****(*) 1
    | RDER BY s.grad_pbroj ASC
    |
    | --
    |
    | Remove the "x" from my email address
    | Jerry Stuckle
    | JDS Computer Training Corp.
    | jstucklex@attglobal.net
    |

Re: sql counting duplicates


max 4000 letters.
Your nickname that display:
In order to stop the spam: 9 + 8 =
QUESTION ON "Databases"

EMSDN.COM