Overview: I'm trying to join several tables together to create a text string built from the values of the extracted data. I've reached a point where I have encountered the need to pull the meta_value
of the meta_key
fields that equal _sku
and _price
. However, I can't figure out how this is done. Any help pointing me in the right direction is very much appreciated.
Specifics: I want to return a row for each possible product and vehicle configuration by creating a string that is structured like this:
SKU, Year, Make, Model, Engine, Title, Price
I have the correct number of results, but I can't figure out how to get the _price
and _sku
values from the wp_postmeta
table.
Final output should look like this:
2T013-ADU00, 2013, Kia, Optima, Hybrid, All Weather Floor Mats, 65.60
2T013-ADU00, 2013, Kia, Optima, SX, All Weather Floor Mats, 65.60
2T013-ADU00, 2013, Kia, Optima, EX, All Weather Floor Mats, 65.60
2T013-ADU00, 2013, Kia, Optima, LX, All Weather Floor Mats, 65.60
2T013-ADU00, 2012, Kia, Optima, LX, All Weather Floor Mats, 65.60
2T013-ADU00, 2012, Kia, Optima, EX, All Weather Floor Mats, 65.60
2T013-ADU00, 2012, Kia, Optima, SX, All Weather Floor Mats, 65.60
2T013-ADU00, 2012, Kia, Optima, Hybrid, All Weather Floor Mats, 65.60
988503W000, 2011, Kia, Sportage, Base, Wiper Blade, 15.75
988503W000, 2011, Kia, Sportage, EX, Wiper Blade, 15.75
Here is my PHP :
$sql = "
SELECT filter.filterId as Filter, posts.*, model.ModelName as Model, make.MakeName as Make, year.Year as Year, engine.EngineName as Engine, meta.*
FROM wp_sFilter filter
JOIN wp_posts posts ON filter.productId = posts.ID
JOIN wp_sModel model ON filter.modelId = model.modelId
JOIN wp_sMake make ON filter.makeId = make.makeId
JOIN wp_sYear year ON filter.yearId = year.yearId
JOIN wp_sEngine engine ON filter.engineId = engine.engineId
JOIN wp_postmeta meta ON filter.productId = meta.post_id
WHERE post_status = 'publish'
AND
post_type = 'product'
AND
meta_key = '_price' OR '_sku'
";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo $row['meta_value'].', '.$row['Year'].', '.$row['Make'].', '.$row['Model'].' '.$row['Engine'].', '.$row['post_title'].', '.$row['meta_value'] . '<br />';
}
And here's the mysql tables :
wp_posts
ID | post_status | post_type | post_title ---+------------------------------------------------- 9 | published | product | All Weather Floor Mats 11 | published | product | Wiper Blade
wp_sFilter
filterId | productId | makeId | modelId | yearId | engineID ---------+-----------+--------+---------+--------+--------- 8 | 9 | 1 | 1 | 3 | 12 7 | 9 | 1 | 1 | 3 | 17 6 | 9 | 1 | 1 | 3 | 11 5 | 9 | 1 | 1 | 3 | 10 9 | 9 | 1 | 1 | 2 | 5 10 | 9 | 1 | 1 | 2 | 6 11 | 9 | 1 | 1 | 2 | 8 12 | 9 | 1 | 1 | 2 | 9 13 | 11 | 1 | 4 | 5 | 13 14 | 11 | 1 | 4 | 5 | 14
wp_sMake
makeId | MakeName -------+--------- 1 | Kia
wp_sModel
modelId | makeId | ModelName --------+--------+---------- 1 | 1 | Optima 4 | 1 | Sportage
wp_sYear
yearId | makeId | modelId | Year -------+--------+---------+----- 1 | 1 | 1 | 2011 2 | 1 | 1 | 2012 3 | 1 | 1 | 2013 4 | 1 | 1 | 2014 5 | 1 | 4 | 2011 6 | 1 | 4 | 2012 7 | 1 | 4 | 2013
wp_sEngine
engineId | makeId | modelId | yearId | EngineName ---------+--------+---------+--------+----------- 1 | 1 | 1 | 1 | LX 2 | 1 | 1 | 1 | EX 3 | 1 | 1 | 1 | SX 4 | 1 | 1 | 1 | Hybrid 5 | 1 | 1 | 2 | LX 6 | 1 | 1 | 2 | EX 8 | 1 | 1 | 2 | SX 9 | 1 | 1 | 2 | Hybrid 10 | 1 | 1 | 3 | LX 11 | 1 | 1 | 3 | EX 12 | 1 | 1 | 3 | Hybrid 13 | 1 | 4 | 5 | Base 14 | 1 | 4 | 5 | EX 15 | 1 | 1 | 1 | LX 16 | 1 | 4 | 5 | SX 17 | 1 | 1 | 3 | SX
wp_postmeta
meta_id | post_id | meta_key | meta_value --------+---------+----------+------------ 20 | 9 | _sku | 2T013-ADU00 24 | 9 | _price | 65.60 50 | 11 | _sku | 988503W000 24 | 11 | _price | 15.75
One way of getting the price and the sku from the wp_postmeta
table is to join to wp_postmeta
table twice.
So you would do something like
...
JOIN wp_postmeta sku ON filter.productId = sku.post_id
JOIN wp_postmeta price ON filter.productId = price.post_id
...
And replace meta.*
in your SELECT
with sku.meta_value as sku, price.meta_value as price
Adjust other parts or the code/sql accordingly, but hopefully you get the idea.
Basically the idea is you need to join the table as the sku and as the price. Imagine you had a sku and price table, how would you do the join, same idea as far as the join is concerned, they are just in the same table.
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.