0

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 'days' based on a query generated by a form (in this stage the query will only contain the 'from' and 'to' countries).

My problem is that i have no idea on how to make select use conditional logic so it picks 'days' from every 'item_id' that has a matching 'from' and 'to'.

(well the other problem is that i don't know any good keywords or actually how this is called, so please forgive me, i only did some blind googling and tried to find similar problems here)

example

7
  • could you give an example of your input (from to) and what you are expecting as a result ? (I don't really understand your question) Commented Jul 4, 2012 at 16:09
  • 1
    Why have you structure the table like this? Looks more like you're using the database as a key-value store to impose your own meta-database on top of it, which negates the purpose of having a relational database in the first place. Commented Jul 4, 2012 at 16:10
  • 1
    This is not a proper relation. Column meta_value mixes values of different types. Your table needs columns id, origin, destination, date, days, userid or similar to represent your information. Commented Jul 4, 2012 at 16:14
  • 1
    This is called EAV, and is generally not a good way to store data in a RDBMS. Commented Jul 4, 2012 at 16:16
  • I think I recognise this table structure. Is it from the Sobi2 Joomla directory component? Commented Jul 4, 2012 at 16:19

1 Answer 1

2
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.

Sign up to request clarification or add additional context in comments.

1 Comment

It seem like your query is a lot simple than mine. Thumb up!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.