+ Reply to Thread
Results 1 to 6 of 6

Thread: MySQL query

  1. #1
    bdistler's Avatar
    bdistler is offline x10 Lieutenant bdistler is an unknown quantity at this point
    Join Date
    May 2010
    Location
    Catalina AZ USA
    Posts
    349

    MySQL query

    Need to make a query for:

    database with 3 tabels

    #1 `shipTo` has key "shipID" and column "address"

    #2 `cust` has key "custID" and fk "shipTo.shipID"

    #3 `parts` has key "partID" and fk "cust.custID"

    list shipto.address
    where [any]cust.shipID = shipTo.shipID WITH [any]parts.custID = cust.custID

    or say it this way

    if any group of `cust` have `parts` throw shipTo.address for that group

    output S/B <= number of `shipTo` rows

    Thanks
    Last edited by bdistler; 10-21-2010 at 07:00 PM.

  2. #2
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: MySQL query

    are you using PDO to handle your db queries?
    I ask because I would have to tweak the query if you did.

  3. #3
    bdistler's Avatar
    bdistler is offline x10 Lieutenant bdistler is an unknown quantity at this point
    Join Date
    May 2010
    Location
    Catalina AZ USA
    Posts
    349

    Re: MySQL query

    Quote Originally Posted by diabolo View Post
    are you using PDO to handle your db queries?
    I ask because I would have to tweak the query if you did.
    Doing debug in phpMyAdmin

    work will be in PHP/MySQL (no PDO)

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: MySQL query

    There are a few point which are unclear. What do you mean by "group", since nothing else suggests the need for grouping? What is "S/B"?

    If I understand the requirements, a (natural) inner join is what you need:

    Code:
    SELECT shipTo.address
        FROM parts
        JOIN cust ON parts.custID = cust.custID
        JOIN shipTo ON shipTo.shipID = cust.shipID
    or
    Code:
    SELECT shipTo.address
        FROM parts
        NATURAL JOIN cust
        NATURAL JOIN shipTo
    What's the full schema (CREATE statements) for these tables? There are a few aspects (such as parts referring to customers) that make me suspect the design is flawed.

    If by "no PDO" you mean you're using the outdated mysql driver, don't. The only reason it's still around is to support legacy code.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    bdistler's Avatar
    bdistler is offline x10 Lieutenant bdistler is an unknown quantity at this point
    Join Date
    May 2010
    Location
    Catalina AZ USA
    Posts
    349

    Re: MySQL query

    Quote Originally Posted by misson View Post
    There are a few point which are unclear. What do you mean by "group", since nothing else suggests the need for grouping? What is "S/B"?

    If I understand the requirements, a (natural) inner join is what you need:

    Code:
    SELECT shipTo.address
        FROM parts
        JOIN cust ON parts.custID = cust.custID
        JOIN shipTo ON shipTo.shipID = cust.shipID
    or
    Code:
    SELECT shipTo.address
        FROM parts
        NATURAL JOIN cust
        NATURAL JOIN shipTo
    What's the full schema (CREATE statements) for these tables? There are a few aspects (such as parts referring to customers) that make me suspect the design is flawed.

    If by "no PDO" you mean you're using the outdated mysql driver, don't. The only reason it's still around is to support legacy code.
    fk = foreign key

    group as cust.custID = 1, cust.custID = 5, cust.custID = 9 All have the same
    fk cust.shipID = 7
    which would throw one row from shipTo (where shipTo.shipID = 7) to the output
    I was saying grouping as a "group of" not as in MySQL

    S/B = should be

    output can be less then or the same as the number of rows in `shipTo`

    I should have named "parts" as "partOrder"

    it is the other way-around customers can have many "partOrder"s

    PDO = PHP Data Objects

    I made this small database outline to work on the 'query'
    for a large database

    Thanks

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: MySQL query

    "fk" is a fairly standard abbreviation, and "PDO" is more standard than the full name; you don't need to expand on those.

    Quote Originally Posted by bdistler View Post
    group as cust.custID = 1, cust.custID = 5, cust.custID = 9 All have the same
    fk cust.shipID = 7
    which would throw one row from shipTo (where shipTo.shipID = 7) to the output
    I was saying grouping as a "group of" not as in MySQL
    [..]
    it is the other way-around customers can have many "partOrder"s
    So (cust.shipID, shipTo.shipID) is a many-to-one relationship, as is (partOrder.custID, cust.custID). That would have been useful information to have from the start.

    From that, it seems you do want SQL grouping. Add a "GROUP BY shipTo.shipID" clause to each of the previous queries. Note that this only works with MySQL, since shipTo.address isn't aggregated. It also assumes that shipTo.address is functionally dependent on shipTo.shipID, which should be the case, since shipTo.shipID should be a superkey.

    Quote Originally Posted by bdistler View Post
    I should have named "parts" as "partOrder"
    A couple of the table and column names could use improvement.
    Last edited by misson; 10-23-2010 at 11:33 PM.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. PHP/MySQL Query Help
    By froger in forum Programming Help
    Replies: 15
    Last Post: 09-04-2010, 10:44 PM
  2. MySQL query sometimes worsks
    By wjh2303 in forum Programming Help
    Replies: 3
    Last Post: 04-06-2009, 11:32 AM
  3. MySQL Databases/SQL query How to?
    By Anfrany1 in forum Free Hosting
    Replies: 3
    Last Post: 11-04-2008, 10:28 PM
  4. mysql query question
    By garrettroyce in forum Programming Help
    Replies: 8
    Last Post: 05-13-2008, 12:29 AM
  5. MySQL query
    By BigEmmaFanz in forum Scripts & 3rd Party Apps
    Replies: 4
    Last Post: 05-23-2005, 11:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers