I would like some help with the following:
(my description will possibly seem weird, example picture below :)
in order to generate a graph, i want to select the
SELECT days.meta_value
FROM tableName AS from_country,
tableName AS to_country,
tableName AS days
WHERE from_country.item_id = to_country.item_id
AND from_country.item_id = days.item_id
AND from_country.field_id = 90
AND to_country.field_id = 93
AND days.field_id = 251
You can add more restrictions if you want to filter by from_country.meta_value
or similar. And you should replace tableName
with the actual name of the table.
Think of from_country
, to_country
and days
as three different pointers to rows in your table, or alternatively different variables taking values from your relation. You want all three of them to describe the same item, and you also want each of them to refer to the field associated with its name. This results in the conditions stated above.
You could even create a view, in order to access this badly designed table like a properly designed one:
CREATE VIEW viewName AS
SELECT item.item_id AS item_id,
from_country.meta_value AS from_country,
to_country.meta_value AS to_country,
days.meta_value AS days
FROM (SELECT DISTINCT item_id FROM tableName) AS item
LEFT JOIN tableName AS from_country
ON (from_country.item_id = item.item_id AND
from_country.field_id = 90)
LEFT JOIN tableName AS to_country
ON (to_country.item_id = item.item_id AND
to_country.field_id = 93)
LEFT JOIN tableName AS days
ON (days.item_id = item.item_id AND
days.field_id = 251)
This would create a view with four columns, from which you could simply select:
SELECT days FROM viewName WHERE from_country LIKE 'A%'
or whatever you want to select. Note that due to the left joins, missing values for some items will result in NULL
values. This is in contrast to the query above, which will omit any item which doesn't have all three values specified. Use whatever is more appropriate in your situation.