A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: SQL syntax- two tables, Join?

  1. #1
    Junior Member scudsucker's Avatar
    Join Date
    Feb 2003
    Location
    Cape Town, RSA
    Posts
    1,509

    SQL syntax- two tables, Join?

    Hi-
    I have two tables in a db on a SQL server. Lets name them tblCompanies and tblDirectors.

    The structure can be simplified to
    Code:
    tblCompanies: intCompanyID (int IDENTITY) and vchCompanyName (varChar)
    and
    Code:
    tblDirectors: intDirectorID (int IDENTITY) and intCompanyID (int) and vchDirectorName (varchar)
    How do I write a SQL statement that will return a list of the companies, and only the first DirectorName that has a corresponding companyID?

    I'm curently using this:
    Code:
    SELECT DISTINCT 
    tblCompanies.intCompanyID, tblCompanies.vchCompanyName,
    tblDirectors.intDirectorID, tblDirectors.vchDirectorName, 
    FROM tblCompanies, tblDirectors
    WHERE 	
    tblCompanies.intCompanyID=tblDirectors.intDirectorID
    But if, say, there are 3 directors per company, the recordset repeats the company ID and name 3 times each.

    Any suggestions? Ive tried INNER and OUTER, LEFT and RIGHT JOINS, but cannot get the required result.
    Hariyemadzisawira nhaka yedu! Down the SCUD and win!
    I'm too lazy to read Private Messages.

  2. #2
    have you tryed something like:

    SELECT DISTINCT
    tblCompanies.intCompanyID, tblCompanies.vchCompanyName,
    tblDirectors.intDirectorID, tblDirectors.vchDirectorName,
    FROM tblCompanies LEFT JOIN tblDirectors ON tblCompanies.intCompanyID=tblDirectors.intDirector ID
    Avataur (Flash) Graphical Chat
    www.avataur.com

  3. #3
    when there are more then 1 director, it is above my SQL ablitys, more over, i think its not a limit to mySQL, its a limit to PHP

    so your best is to do 2 actual querys..

    Iv crafted SQL thats done what you want do .. in things like Access, but i cant get it to work with php/mysql

    Its possable this is fixed in php5 with mysql4, there is a hole new more advanced exention, and a handful of new commands in mysql4 as well.
    Last edited by ZorTiger; 09-17-2004 at 11:11 AM.
    Avataur (Flash) Graphical Chat
    www.avataur.com

  4. #4
    Registered User
    Join Date
    Feb 2001
    Posts
    13,039
    Hi,

    since mysql does not support the
    select distinct on (...)
    clause, your option would be to loop over all results in php and show just the first one for each company

    like
    $cid = 0;
    while($row = mysql_fetch_row($result))
    { if($cid != $row[0])
    { // print result here, then
    $cid = $row[0];
    }
    }

    Musicman

  5. #5
    actually, i came up with it dose allow select distinct

    if you search on mysql' site for SELECT Syntax

    you will come up with this:
    http://dev.mysql.com/doc/mysql/en/SELECT.html

    SELECT
    [ALL | DISTINCT | DISTINCTROW ]
    [HIGH_PRIORITY]
    [STRAIGHT_JOIN]
    [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
    [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr, ...
    [INTO OUTFILE 'file_name' export_options
    | INTO DUMPFILE 'file_name']
    [FROM table_references
    [WHERE where_definition]
    [GROUP BY {col_name | expr | position}
    [ASC | DESC], ... [WITH ROLLUP]]
    [HAVING where_definition]
    [ORDER BY {col_name | expr | position}
    [ASC | DESC] , ...]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [PROCEDURE procedure_name(argument_list)]
    [FOR UPDATE | LOCK IN SHARE MODE]]


    How ever this could be doc for version 4. It seems like Mysql's featured tripled in it.
    Last edited by ZorTiger; 09-17-2004 at 12:01 PM.
    Avataur (Flash) Graphical Chat
    www.avataur.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center