+ Reply to Thread
Results 1 to 3 of 3

Thread: Dealing with NULL in MySQL Query

  1. #1
    lostcommander's Avatar
    lostcommander is offline x10Hosting Member lostcommander is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    United States of America
    Posts
    52

    Question Dealing with NULL in MySQL Query

    Short Version: Is there some way to get a single integer value out of a set of columns when it is known that all but 1 will be null? I have tried simply adding them with +, and SUM, MAX, MIN, etc. all only work with GROUP BY stuff. e.g.
    (tableB.valueX + tableC.valueY) AS value
    MAX(tableB.valueX, tableC.valueY) AS value
    ...
    Is there a way to select out the non-null value or to combine away the NULL values?


    Long Story: I have 3 tables. Table A is the primary table and has an ID. Tables B and C contain additional information about rows of table A and each B or C row has an ID matching it to a row in table A. Every row in table A matches exactly 1 row in either table B or table C, never both. Tables B and C share a column which I would like to have SELECT-ed out under a single column (but they do not share with table D, E... so moving this column to table A is not a solution). Currently I am simply Left JOIN-ing everything and then finding the existing value on the PHP side of things, but it is sort of ugly and will be hard to maintain if/as more such info tables are added.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Dealing with NULL in MySQL Query

    Code:
    SELECT SUM/MAX/MIN as value from A inner join B on A.id = B.foreignKey
    UNION
    SELECT SUM/MAX/MIN  as value from A inner join C on A.id = C.foreignKey
    Sould return two rows. Add or take max/min
    Last edited by descalzo; 12-09-2009 at 04:21 PM.

  3. #3
    lostcommander's Avatar
    lostcommander is offline x10Hosting Member lostcommander is an unknown quantity at this point
    Join Date
    Apr 2009
    Location
    United States of America
    Posts
    52

    Re: Dealing with NULL in MySQL Query

    Yup, that gets is. I had not remembered/thought of using INNER JOIN twice, plus the UNION. Makes for a horribly ugly query, but at least it keeps the complication all in one place. Thanks!

+ Reply to Thread

Similar Threads

  1. Mi propio CMS
    By evigra in forum Tutoriales
    Replies: 38
    Last Post: 09-12-2009, 01:28 PM
  2. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  3. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  4. Why does this happen?
    By sophon_more in forum Free Hosting
    Replies: 10
    Last Post: 12-07-2007, 04:07 PM
  5. Mod Help
    By Ericsson in forum Free Hosting
    Replies: 4
    Last Post: 03-04-2005, 03:49 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