Wordpress SQL data query layout - mysql

I am using SQL Executioner in Wordpress Admin and trying to get a list of data from a table (wp_postmeta) using the following:
SELECT post_id, meta_value from wp_postmeta
WHERE meta_key = "_wprm_reservation_name"
UNION
(SELECT post_id, meta_value from wp_postmeta
WHERE meta_key = "_wprm_reservation_phone_number");
This returns the information that I require but the layout is wrong. I am getting two columns: one with the post_id number and meta_value (persons name) listed then after all those I am getting the post_id number repeated and the telephone number.
Ideally I am trying to get the phone number to be placed next to the persons name. The post_id is the key as the number is used for both the name and phone number.
I have tried number variations trying to understand the UNION statement but cannot get this to work. Can anyone advise please where I am going wrong here?

Union working like this, see the documentation. What you need is a subquery, or a join. This is with subquery:
SELECT wppm.post_id, wppm.meta_value,
(SELECT wppmphone.meta_value FROM wp_postmeta wppmphone
WHERE wppmphone.meta_key = "_wprm_reservation_phone_number"
AND wppmphone.post_id = wppm.post_id) AS phoneNum
FROM wp_postmeta wppm WHERE meta_key = "_wprm_reservation_name";
This is one query, I just separated the subquery by new lines to be readable. This is not tested, but it should like something like this.
Note, I am using aliases for the table names.

Related

mysql combined query, ids from comma separated list

I am not that familiar with mysql, so do not exactly not how sample the query correctly. I am working with wordpress and to illustrate what I would like to select please see the image below:
I have the ID of one special post. I would like to get all other Posts that are connected to it in the following way:
In the posts_meta table can exist entries, whose post_id matches the given ID. One of these entries meta_key is of a certain value and the meta_value of that very entry is a comma separated list of post_id's. I would like to Select * for each of Posts whose ID is in that comma separated list
I started with the following:
SELECT *
FROM prefix_posts AS a
JOIN prefix_postmeta AS b ON ( a.ID = b.post_id )
WHERE … //??
but I have no idea how to finish the query. How can I select the ids, to select posts in turn? I found SO answers dealing with FIND_IN_SET, but I do not want to test if a given ID is in the set, I the more need something like »STR_SPLIT« in a subquery.
I would like to Select * for each of Posts whose ID is in that comma separated list
Why not using FIND_IN_SET method ?, it can help you for your problem
This query return all posts whose have id in meta_value field from meta row having your "special post id" as post_id.
SELECT * FROM Post a, meta b
WHERE FIND_IN_SET(a.id, b.linked)>0 AND b.post_id= $id_post;
Tried on SQLFiddle, it seems to work.
Add a GROUP BY clause if you want avoid duplicates.

SQL joining wordpress tables

I am struggling with an SQL query to join 3 tables to return specific results.
Anyone familiar with wordpress may be able to assist as I am using word press to power post interactions with the db behind the scenes but am building a custom UI. The three tables are: -
posts (**ID**, post_title, post_content, post_modified_gmt)
term_relationships (**object_id**, term_taxonomy)
terms (**term_id**, name, slug)
I have got as far as..
SELECT posts.post_title FROM posts
INNER JOIN term_relationships
ON posts.ID = term_relationships.object_id
Which returns a list of all the post titles that have a matching ID number in term_relationships. However term names are in the table 'terms' and the ID's don't match the other two tables. In the terms table the term_id refers to the name of the term, e.g
term_id = 2, name = blog
Basically I am trying to achieve a query whereby if I set the term_id = 2 it returns all the rows from the table posts that have the term relationship to blog, meaning the query returns all blog posts and I am completely lost!
Can anyone give me a few pointers? my mind is boggled.
I managed to achieve the result I was looking for with the following query, but would still appreciate some pointers should anyone stumble upon this and have the inclination :)
SELECT * FROM posts
INNER JOIN term_relationships
ON posts.ID = term_relationships.object_id
WHERE term_relationships.term_taxonomy_id = 2
AND posts.post_type = 'post'
AND posts.post_status = 'publish'
ORDER BY posts.post_modified_gmt ASC
LIMIT 0,5

Is there a more efficient solution for this MySQL Query?

I am working on extracting and displaying data from a Wordpress DB to a mobile app for a customer and I am having a little trouble refining this query to be most efficient.
In wordpress, there are three tables that link the data I need to access
1. wp_posts - in this table there is the main post title, it's published status and the post type.
2. wp_postmeta - this table has all supplemental info related to the post id in the above table.
3. wp_p2p - this table has links to all the parent-child posts and their relationship.
Because of the volume of data in these tables, the query I currently have takes about 13 seconds to run, could you please take a look at this sqlfiddle and let me know what I could look at to improve it? The query in it's current form is not the end result, but improving it will improve my end result. I also need to add a search field on the "name" in the wp_postmeta table.
http://sqlfiddle.com/#!2/0e9e0/1
Any direction is appreciated, thank you!
If I understand correctly, you're looking for only child posts, in which case, the query below should be much faster:
SELECT wp_posts.id, post_title, post_status, post_type
FROM wp_posts
JOIN wp_postmeta ON (wp_posts.id = wp_postmeta.post_id)
LEFT JOIN wp_p2p ON (wp_posts.id = wp_p2p.p2p_from)
WHERE `post_status`='publish' AND `post_type`='merchant'
AND wp_p2p.p2p_from IS NULL
GROUP BY wp_posts.id
This query will be optimized to find where a match doesn't exist in the p2p table so that part will be much faster than how you're currently doing it. It looks like you can also remove the JOIN on wp_postmeta since you don't use it at all. Removing that JOIN would also make the GROUP BY redundant and removing it could help the performance a little. Removing the GROUP BY would also be a good practice since strictly you can't select non-aggregate fields that aren't in the GROUP BY clause, but MySQL provides for this functionality so the query will still work either way.
To begin with, you should join tables using INNER or OUTER JOIN syntax. So:
FROM wp_posts INNER JOIN wp_postmeta ON wp_posts.id = wp_postmeta.post_id
or perhaps you want an OUTER JOIN here?
Can you explain why you're doing a GROUP BY on wp_posts.id. Does this column not have unique values?

SQL: Trying to select two meta_values in one query

SELECT meta_value
FROM wp_postmeta
WHERE meta_key = "bid_resource_lat"
AND meta_key = "bid_resource_lng"
I'm trying to select two rows within one query, as you can see above ... and I can't wrap my head around why this isn't working...
And how would I make sure that the two results are grouped together within a row?
You need "OR" as the logic is the wrong way round from as you're imagining it.
SELECT meta_value FROM wp_postmetaWHERE meta_key = "bid_resource_lat" OR meta_key = "bid_resource_lng"
Explanation:
You want to get a row if the key is either the first OR if it's the second. This returns up to two rows. The way you wrote it then both conditions needed to be matched.
"I want a car that is italian OR expensive" - You can get a Ferrari, a Mercedes and a Fiat
"I want a car that is italian AND expensive" - You can get a Ferrari
You want to use an OR condition, not an AND condition:
SELECT meta_value
FROM wp_postmeta
WHERE meta_key = "bid_resource_lat"
OR meta_key = "bid_resource_lng"
Your query could not return any rows, because you're asking for records where the meta_key column is equal to both "bid_resource_lng" and "bid_resource_lng", which can obviously never happen since one row will only have one value in the meta_key column.
meta_key = "bid_resource_lat" AND meta_key = "bid_resource_lng"
Above will never be TRUE. Use OR instead
meta_key cannot be bid_resource_lat and bid_resource_lng at the same time.

Mysql Query - Joins causing confusion for me in this query

I have a query that returns results related to items that match a specific category...
There are 3 mysql tables that results to this, items, categories and item_categories.
These i assume are self explanatory, but the latter, is a linking table that links any specific item to any specific category, using a match of id's.
The items table contains one row, with an id value of 1.
The categories table is filled with 15 rows, with id values of 1-15.
the item_categories table contains one row, the item_id value is 1 and the category_id value is 5.
This is the mysql query in its php form:
$catResultQuery = "
SELECT i.id, name, price
FROM items i
INNER JOIN item_categories
ON i.id = item_id
INNER JOIN categories c
ON category_id = c.id
WHERE MATCH (c.id)
AGAINST ('{$_SESSION['input']}' IN BOOLEAN MODE)
ORDER BY name
";
The session variable has a value of 5, but for some reason, this query displays a 0 result set.
Even when i run the query in php myadmin, it returns 0 rows.
And i am confused, because in my head, the logic behind all of this seems fairly simple, but for some reason i get 0? Does anyone have any idea where i have gone wrong with this?
Any advice and input would be greatly appreciated, thank you!!
Ok, I see now that you're building the SQL dynamically. If that's the case, then this should work:
SELECT i.id, name, price
FROM items i
INNER JOIN item_categories
ON i.id = item_id
INNER JOIN categories c
ON category_id = c.id
WHERE c.id
IN ('{$_SESSION['input']}')
ORDER BY name
Just make sure '{$_SESSION['input']}' is comma delimited and be aware that this carries the risk of SQL injection because you're constructing the SQL on the fly.