product_to_category
p2c
contains 2 columns:
category_id product_id
This table may contain more than one entry for each product so MAX (category_id)
is needed.
category_to_google
c2g
contains 2 columns:
category_id google_id
google_category
gc
contains 2 columns:
google_id name
So get MAX(category_id) FROM p2c,
get google_id FROM c2g WHERE category_id = selected category_id,
finally get name from gc WHERE google_id = selected google_id
I can't seem to get the join right.
ANSWER:
$query = $this->db->query("
SELECT name FROM {$this->prefix}google_category
WHERE google_id = (
SELECT google_id FROM {$this->prefix}category_to_google
WHERE category_id = (
SELECT MAX(category_id) FROM {$this->prefix}product_to_category
WHERE product_id = '" . (int)$product_id . "'
)
)");
This works, thanks RC.
Is it possible to get this into an nice join, it has to be nested into an existing query?
I'm not sure I did get what you want but I think:
SELECT name FROM gc WHERE google_id = (
SELECT google_id FROM c2g WHERE category_id = (
SELECT MAX(category_id) FROM p2c
)
);
might be what you are searching for.
Maybe something like this...
SELECT name
FROM google_category c
JOIN category_to_google cg
ON cg.google_id = c.google_id
JOIN (SELECT MAX(category_id) max_category_id
FROM product_to_category
WHERE product_id = $product_id) x
ON x.max_category_id = cg.category_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.