Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,873
    Plugin Contributions
    96

    Default Help with a MySQL query

    I've got the following SQL query:
    Code:
    SELECT pa.*, pad.*, p.products_model, po.products_options_name, pov.products_options_values_name 
    FROM products AS p, products_options AS po, products_options_values AS pov, products_attributes AS pa LEFT JOIN products_attributes_download AS pad ON (pa.products_attributes_id = pad.products_attributes_id) 
    WHERE pa.products_id = p.products_id
    AND pa.options_id = po.products_options_id
    AND po.language_id = 1
    AND pa.options_values_id = pov.products_options_values_id
    AND pov.language_id = 1 
    ORDER BY pa.products_id ASC, pa.options_id ASC, pa.options_values_id ASC
    What I'm trying to do is retrieve a single row for each attribute that contains the information from both the products_attributes and products_attributes_download tables (in addition to a couple of "defining" fields).

    The query is working except that the products_attributes_id value is coming back as "NULL" unless there's a companion record in the products_attributes_download table, in which case it's properly returning the numerical attributes_id. Is there some MySQL "magic" that will cause the result to be returned with those index values, i.e. so the query works the same as when it's run via phpMyAdmin?

  2. #2
    Join Date
    Jul 2012
    Posts
    16,816
    Plugin Contributions
    17

    Default Re: Help with a MySQL query

    Try swapping your original sequence of fields to export: pa.* and pad.*. Both tables have products_attributes_id associated with them, it looks like the later replaces the former. Of course the "clear" answer is to include on the select only the fields that are desired to be returned, not everything...
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  3. #3
    Join Date
    Jan 2004
    Posts
    66,443
    Plugin Contributions
    279

    Default Re: Help with a MySQL query

    lat9,

    A couple questions:

    a) what exactly is the difference you're seeing between how phpMyAdmin returns the results and how "whatever else" returns them?
    (cuz, unless something is intercepting and overriding exactly what the query is doing, the results should be the same ... if the query is the same. It seems suspicious that you're suggesting you're getting different results from different tools running the same query)

    b) Your query is a left/inner join on multiple tables. By the very nature of such a query, NULL is returned when companion records don't exist.
    Are you saying you want to "always have all attributes, regardless of whether a download exists, and if a download exists then ALSO return its info"?

    Where are you using this query? Some context may help determine the ultimate solution.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  4. #4
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,873
    Plugin Contributions
    96

    Default Re: Help with a MySQL query

    @mc12345678, you're spot-on. I modified the query to select only the pa.products_attributes_id (so the pad version doesn't overwrite it) and now the products_attributes_id values aren't being nulled-out when there's no matching record in the products_attributes_download table.

    @DrByte, the difference that I'm seeing (even with the previous all-fields select) between phpMyAdmin and my under-development database I/O handler is that phpMyAdmin always returns the non-NULL version of the products_attributes_id while the $db execution of that same sQL query returns NULL as the id value if no matching record is found in the joined table.

    What I'm looking to return is a full products_attributes+products_attributes_download field-list, with the download table's fields set to NULL if not present.

    Like I indicated, the query's working as I wanted now with the removal of the pad.products_attributes_id value as one of the selected fields. It is still a mystery to me why the values returned by phpMyAdmin and the mysqli interfaces are different.

  5. #5
    Join Date
    Jan 2004
    Posts
    66,443
    Plugin Contributions
    279

    Default Re: Help with a MySQL query

    Quote Originally Posted by lat9 View Post
    Like I indicated, the query's working as I wanted now with the removal of the pad.products_attributes_id value as one of the selected fields. It is still a mystery to me why the values returned by phpMyAdmin and the mysqli interfaces are different.
    If phpMyAdmin is actually showing ALL fields for ALL tables (and NOT collapsing/overwriting the ones with duplicate field names) then that would explain the difference.
    The way ZC is designed to bring back an associative array, which causes duplicates to overwrite one another could be the problem you were inadvertently running into.
    If you don't want those collisions, simply name each field uniquely with an "as" clause. In most cases the "collision" is harmless, and intentionally not an issue.
    But in the case of your desired output, reversing the order is probably the simplest and best approach.


    The "as" clause I mention would be something like "select po.products_attributes_id, pa.products_attributes_id as pa_id ..." ... so that the second one is given a new associative-array entry (of pa_id in this example). Then you can access that unique field just like any other.
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

  6. #6
    Join Date
    Sep 2009
    Location
    Stuart, FL
    Posts
    13,873
    Plugin Contributions
    96

    Default Re: Help with a MySQL query

    Ah, that explains it! Thanks, DrByte.

  7. #7
    Join Date
    Jul 2012
    Posts
    16,816
    Plugin Contributions
    17

    Default Re: Help with a MySQL query

    Besides that, a majority of ZC's sql statements using multiple tables are "straight" joins rather than left joins, so only records that are in both (all) tables are returned, or as one of the two original suggestions, just the field(s) in question are returned.

    I am curious about the reversal of the two pa.* and pad.* (making them pad.* first and then pa.*) to see if/that the originally expected results are returned. Very interesting...
    ZC Installation/Maintenance Support <- Site
    Contribution for contributions welcome...

  8. #8
    Join Date
    Nov 2005
    Location
    los angeles
    Posts
    2,908
    Plugin Contributions
    13

    Default Re: Help with a MySQL query

    Quote Originally Posted by lat9 View Post
    I've got the following SQL query:
    Code:
    SELECT pa.*, pad.*, p.products_model, po.products_options_name, pov.products_options_values_name 
    FROM products AS p, products_options AS po, products_options_values AS pov, products_attributes AS pa LEFT JOIN products_attributes_download AS pad ON (pa.products_attributes_id = pad.products_attributes_id) 
    WHERE pa.products_id = p.products_id
    AND pa.options_id = po.products_options_id
    AND po.language_id = 1
    AND pa.options_values_id = pov.products_options_values_id
    AND pov.language_id = 1 
    ORDER BY pa.products_id ASC, pa.options_id ASC, pa.options_values_id ASC
    i'm a little late to the party on this one. but the path you are traveling is fraught with danger....

    i will not get into the specifics on what you are trying to accomplish, but the construction of your SQL statement is bound to cause you grief. whenever you use multiple tables in the FROM statement where the join characteristic is in the WHERE statement, you should NEVER combine that with another table using a LEFT JOIN, INNER JOIN or any other JOIN statement. you should stick to one style else you are bound to have problems. (there is a name for the difference in styles, but i can not remember/track it down.)

    i would modify your SQL statement as below.

    Code:
    SELECT pa.*, pad.*, p.products_model, po.products_options_name, pov.products_options_values_name 
    FROM products AS p
    JOIN products_attributes AS pa on pa.products_id = p.products_id
    JOIN products_options AS po on pa.options_id = po.products_options_id
    JOIN products_options_values AS pov on pa.options_values_id = pov.products_options_values_id
    LEFT JOIN products_attributes_download AS pad ON (pa.products_attributes_id = pad.products_attributes_id) 
    WHERE 
    po.language_id = 1
    AND pov.language_id = 1 
    ORDER BY pa.products_id ASC, pa.options_id ASC, pa.options_values_id ASC
    author of square Webpay.
    mxWorks now has Apple Pay and Google Pay. donations: venmo or paypal accepted.
    premium consistent excellent support. available for hire.

 

 

Similar Threads

  1. v154 Quick help with MySQL Query - Selecting OLD Product at Random
    By Jeff_Mash in forum Setting Up Specials and SaleMaker
    Replies: 21
    Last Post: 24 Nov 2015, 12:05 AM
  2. Need help with a mySQL Query
    By jeffmic in forum General Questions
    Replies: 3
    Last Post: 19 Dec 2010, 02:21 PM
  3. Help for mysql query!!
    By g.nencini in forum General Questions
    Replies: 2
    Last Post: 13 Jul 2009, 05:32 AM
  4. Report Query MySQL - Help Needed
    By ksoup in forum General Questions
    Replies: 2
    Last Post: 1 Jul 2008, 03:45 PM

Posting Permissions

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