Pergunta

I have a problem with my SQL query. I am trying to query the database to get data from 3 different tables, where on one table conditions are applied. On the second entry it shows the results correctly, though on the first it lacks to show the results of the table where the conditions are applied.

This is my query:

SELECT
    `".PRODUCTS."`.*,
    `".CATEGORIES."`.*,
    `q_prices`.*
FROM
    `".PRODUCTS."`
LEFT JOIN
    `".CATEGORIES."`
ON
    `".PRODUCTS."`.`category_id` = `".CATEGORIES."`.`category_id`
INNER JOIN(
    SELECT
        `".PRICES."`.*
        MAX(`".PRICES."`.`price_modified`) modified
    FROM
        `".PRICES."`
    GROUP BY
        `".PRICES."`.`product_id`
    ) `q_prices`
ON
    `".PRODUCTS."`.`product_id` = `q_prices`.`product_id`

This is what it returns:

    Array
(
[0] => stdClass Object
    (
        [product_id] => 1
        [product_name] => Test product
        [product_alias] => test-product
        [category_id] => 1
        [product_created] => 2013-07-29 11:36:51
        [product_modified] => 2013-07-29 11:36:51
        [category_name] => Test categorie
        [category_alias] => test-categorie
        [category_parent] => wonenplaza.nl
        [category_created] => 2013-07-29 11:39:29
        [category_modified] => 2013-07-29 11:39:29
        [price_id] => 1
        [price_amount] => 25.00
        [price_tax] => 21
        [price_created] => 2013-07-29 11:38:18
        [price_modified] => 2013-07-29 11:38:18
        [modified] => 2013-07-29 11:38:52
    )

[1] => stdClass Object
    (
        [product_id] => 2
        [product_name] => Priva Blue ID
        [product_alias] => test-product2
        [category_id] => 1
        [product_created] => 2013-07-29 12:18:54
        [product_modified] => 2013-07-29 12:18:54
        [category_name] => Test categorie
        [category_alias] => test-categorie
        [category_parent] => wonenplaza.nl
        [category_created] => 2013-07-29 11:39:29
        [category_modified] => 2013-07-29 11:39:29
        [price_id] => 4
        [price_amount] => 20.00
        [price_tax] => 21
        [price_created] => 2013-07-29 12:19:11
        [price_modified] => 2013-07-29 12:19:11
        [modified] => 2013-07-29 13:30:05
    )
)

I think it has something to do with the limit specified on the LEFT JOIN query, but I'm not sure about that. I don't know how else to query the database to get these results.

Thanks in advance (:

Foi útil?

Solução

Your subquery finds the latest of all prices, no matter the product, which isn't what you need. Fixing it isn't trivial, but you could rewrite your nested query to a LEFT JOIN, which would seem to simplify things; (sorry, your tables are slightly renamed to make SQLfiddle happy)

SELECT `.PRODUCTS.`.*, `.CATEGORIES.`.*, `q_prices`.*
FROM   `.PRODUCTS.`
LEFT JOIN `.CATEGORIES.`
  ON `.PRODUCTS.`.`category_id` = `.CATEGORIES.`.`category_id`
LEFT JOIN `.PRICES.` `q_prices`
  ON `q_prices`.`product_id` = `.PRODUCTS.`.`product_id`
LEFT JOIN `.PRICES.` `dummy`
  ON `dummy`.`product_id` = `.PRODUCTS.`.`product_id`
 AND `q_prices`.`price_modified` < `dummy`.`price_modified`
WHERE `dummy`.`price_modified` IS NULL;

A simple SQLfiddle to test with.

Outras dicas

This is how to get other data associated with most recent values.

select f1, f2, etc
from sometables
join (
select f1, max(datefield) maxdt
from table1
where whatever
group by f1
) temp on table1.datefield = maxdt
and table1.f1 = temp.f1
etc

Apply that principle to your tables and you are good to go. By the way, you want an inner join, not a left join to the derived table.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top