I apologize if I cannot explain this properly in advance but I will do my best.
I have a SQL table. In the table, there are multiple columns. One of which is labeled 'meta_key' and another which is 'meta_value'. I would like to know how to write SQL that will pull specific rows in one column and also gather the data from another column in the same row and separate all of these out into their own columns. (Please forgive me if this doesn't make sense.)
I've tried the following which was provided by a friend:
SELECT meta_key, meta_value
FROM wp_gf_entry_meta
WHERE meta_key = 3 OR meta_key = 9 OR meta_key = 22 OR meta_key = 23
But it only gets me halfway there in that it provides all of the data from those rows and columns and consolidates it all into a single column.
Again, the SQL that was tried was:
SELECT meta_key, meta_value
FROM wp_gf_entry_meta
WHERE meta_key = 3 OR meta_key = 9 OR meta_key = 22 OR meta_key = 23
I didn't receive error messages for above but it did not provide the desired outcome.
Here is a screenshot of the table before the above SQL:
And here is a screenshot after:
UPDATE
I've made some progress. I believe what they call an 'Aggregate Function' is what I can use.
This has gotten me closer to my goal:
SELECT
max(case when meta_key = 3 then meta_value end) Ticker,
max(case when meta_key = 9 then meta_value end) Reason,
max(case when meta_key = 22 then meta_value end) Resistance,
max(case when meta_key = 23 then meta_value end) Support
from wp_gf_entry_meta
The only problem now is that it only returns one row and I'm not certain how to make it return more than one row.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `wp_gf_entry_meta` ADD INDEX `wp_entry_idx_meta_key` (`meta_key`);
SELECT
wp_gf_entry_meta.meta_key,
wp_gf_entry_meta.meta_value
FROM
wp_gf_entry_meta
WHERE
wp_gf_entry_meta.meta_key IN (
3, 9, 22, 23
)