What i want is:
Get posts with date greater then 2010-03-02 and with the meta_value 'something' + like '2010-'
because there are other values like 239048192304 or 293811743
$query = "SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_posts
ON wp_postmeta.post_id=wp_posts.ID
WHERE wp_postmeta.meta_value >='2010-03-02'
AND wp_postmeta.meta_value = 'something'
AND wp_postmeta.meta_value LIKE '2010-'
ORDER BY wp_postmeta.meta_value ASC
LIMIT 0,10";
can you help me out please? thank you!
Update2:
table wp_postmeta
post_id | meta_value
5 | 2010-12-30
5 | Berlin
3 | 2010-12-29
3 | Paris
2 | 2009-12-29
2 | Paris
14 | 12232456521
14 | Berlin
Output:
2010-12-30 Berlin ID 5
2010-12-29 Paris ID 3
Maybe you mean an OR instead of an AND?
...
WHERE wp_postmeta.meta_value >= '2010-03-02' OR
wp_postmeta.meta_value = 'something' OR
wp_postmeta.meta_value LIKE '2010-'
Unfortunately in the English language, AND and OR can be used interchangeably in certain cases:
"I always carry an umbrella for when it rains and snows."
"I always carry an umbrella for when it rains or snows."
The above wouldn't be equivalent for computers :)
A larger data set and sample answer would help clarify the question but here is my interpretation of what you are looking for. It's not elegant but if you've got the buffer space allocated it works.
SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_posts
ON wp_postmeta.post_id=wp_posts.ID
WHERE wp_postmeta.post_id IN (
select post_id from wp_postmeta where str_to_date(meta_value, '%Y-%m-%d') >= 2010-03-02' and post_id in (select post_id from wp_postmeta where meta_value = 'something')
);
For getting post information and their post meta values you need the following query:
SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID,
wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta JOIN wp_posts
ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_date >= '2010-03-02'
AND EXISTS (SELECT 1 from wp_postmeta m1
WHERE m1.post_ised = wp_posts.ID
AND wp_postmeta.meta_value = 'something'
AND EXISTS (SELECT 1 from wp_postmeta m2
WHERE m2.post_ised = wp_posts.ID
AND wp_postmeta.meta_value LIKE '2010-%')
ORDER BY wp_postmeta.meta_value
ASC LIMIT 0, 10
Try this:
$query = "SELECT p.ID, m1.meta_value, m2.meta_value, p.post_title FROM
wp_posts p, wp_postmeta m1, wp_postmeta m2
WHERE p.post_date > '2010-03-02' AND
m1.post_id=p.ID AND m2.post_id=p.ID AND
m2.meta_value LIKE '2010-%' AND
m1.meta_value = 'something'
ORDER BY m1.meta_value, m2.meta_value
LIMIT 0,10";
No need for the distinct, since we're showing everything on one row anyway.
Those statements contradict themselves. First you are asking for it to be >= a date. So is meta_value a date? Then you say it must be equaled to 'something', so now it is a string and not a date. Finally you say have it be like 2010-, so now we are back to a string or a date, but no wild card % in the like so you are basically saying it has to be equaled to 2010- as well.
What is the value stored in meta_value?
Are you sure you do not mean to query multiple fields?
UPDATE
Based on the new information, I think this is what you want:
$query = "SELECT DISTINCT wp_postmeta.meta_key, wp_postmeta.meta_value, wp_posts.ID, wp_posts.guid, wp_postmeta.post_id, wp_posts.post_title
FROM wp_postmeta
INNER JOIN wp_posts
ON wp_postmeta.post_id=wp_posts.ID
WHERE (wp_postmeta.meta_value = 'something'
OR wp_postmeta.meta_value LIKE '2010-%')
ORDER BY wp_postmeta.meta_value ASC
LIMIT 0,10";
Hopefully that is what you were after. I removed the 2010 >= condition given that the 2010 LIKE condition will pull that same data.
Related
UPDATE The pay query is now solved with the help provided in the replies. The answer Strawberry provided is very close. It just needed to be edited a bit as the WHERE clause has a redundancy and incorrect statement that I provided in my original query. Here is the correct, slightly revised query:
SELECT ux.user_id
, ux.meta_value pay
FROM wp_um_groups_members m
Join wp_usermeta ux
ON m.user_id1 = ux.user_id
WHERE ux.meta_key = CONCAT('_um_groups_', m.group_id,'_price')
This matches the following data for my 'Pay' field:
From up_usermeta:
umeta_id | user_id | meta_key | meta_value
===========================================================
622680 | 5989 | _um_groups_47652_price | 500
From wp_um_groups_members:
id | group_id | user_id1 | user_id2 | status | role | invites | time_stamp | date_joined
===============================================================================================================
187 | 47682 | 5989 | 3 | approved | member | 1 | 2020-02-15 10:59:08 | 2020-02-15 10:59:08
I through that in with my original query below, and everything is working.
Original Post
I'm sure I'm making this much more difficult than it needs to be. Below is the flow chart and query. When I add the 'Pay'subquery, I receive results that are not matched correctly. If I remove the "Pay" subquery, it works fine (with exception that there is no pay).
In the database, I have many entries in wp_usermeta.meta_value such as:
meta_value = _um_groups_47859_price
In that example, the number 47859 equals the value of wp_um_groups_members.group_id
So I was hoping to be able to query that exact match using something similar to: LIKE CONCAT('%', wp_um_groups_members.group_id ,'%')
That did not seem to work either.
Here is my current query:
SELECT
wp_um_groups_members.group_id AS ID, wp_posts.post_title AS Gig, Location.Location, Type.`Event Type`, wp_users.display_name AS Player, Date.Date, Pay.Pay
FROM
wp_um_groups_members
Inner Join wp_posts ON
wp_um_groups_members.group_id = wp_posts.ID
Inner Join (SELECT
wp_postmeta.post_id, wp_postmeta.meta_value AS Location
FROM
wp_postmeta
WHERE
wp_postmeta.meta_key = '_um_groups_event_location'
) Location ON
Location.post_id = wp_posts.ID
Inner Join (SELECT
wp_postmeta.post_id, wp_postmeta.meta_value AS `Event Type`
FROM
wp_postmeta
WHERE
wp_postmeta.meta_key = '_um_groups_event_type'
) Type ON
Type.post_id = wp_posts.ID
Inner Join wp_users ON
wp_users.ID = wp_um_groups_members.user_id1
Inner Join (SELECT
wp_postmeta.post_id, wp_postmeta.meta_value AS Date
FROM
wp_postmeta
WHERE
wp_postmeta.meta_key = '_um_groups_event_start'
) Date ON
Date.post_id = wp_posts.ID
Inner Join (SELECT
wp_usermeta.user_id, wp_usermeta.meta_value AS Pay
FROM
wp_um_groups_members
Inner Join wp_usermeta ON
wp_um_groups_members.user_id1 = wp_usermeta.user_id
WHERE
wp_usermeta.meta_key like '%price'
) Pay ON
Pay.user_id = wp_users.ID
WHERE
wp_um_groups_members.status = 'approved'
Is there a way to to modify that last Inner Join in order to contain something like: WHERE
wp_usermeta.meta_value = '_um_groups_47859_price' in which 47859 would is wp_um_groups_members.group_id?
SELECT ux.user_id
, ux.meta_value pay
FROM wp_um_groups_members m
Join wp_usermeta ux
ON m.user_id1 = ux.user_id
WHERE ux.meta_key LIKE '%price'
AND ux.meta_value = CONCAT('_um_groups_', m.group_id,'_price')
Incidentally, while there's no performance benefit, when working with an EAV (like wp_postmeta), I prefer this syntax...
SELECT post_id
, MAX(CASE WHEN meta_key = '_um_groups_event_location' THEN meta_value END) location
, MAX(CASE WHEN meta_key = '_um_groups_event_type' THEN meta_value END) event_type
, MAX(CASE WHEN meta_key = '_um_groups_event_start' THEN meta_value END) date
, MAX(CASE WHEN meta_key = '_um_groups_event_type' THEN meta_value END) event_type
FROM wp_postmeta
GROUP
BY post_id
(Strictly speaking, this is more akin to using LEFT JOIN than INNER JOIN, as per your example)
here is my custom sql query ;
SELECT id,
post_title,
post_content,
comment_count,
post_date,
post_status,
post_name
FROM wp_posts
WHERE post_type = 'post'
AND post_status = 'publish'
AND id NOT IN (SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'visible_headlane'
AND meta_value = 'On')
AND id NOT IN (SELECT DISTINCT post_id
FROM wp_postmeta
WHERE meta_key = 'visible_homepage'
AND meta_value = 'On')
ORDER BY post_date DESC
LIMIT 11, 32
i can not use, LIMIT in NOT IN Query so, there is any way to use, or limit SELECT DISTINCT sql query, or any idea ?
Note : I need to optimise this query because, it takes so long , like a slow query.
Thanks.
Switch to NOT EXISTS( SELECT 1 FROM ... ) -- this variation does not need to compute the entire list of things; it only needs to check for the absence. LEFT JOIN .. ON .. IS NULL is also more efficient.
Furthermore, the standard schema for wp_postmeta is quite inefficient; see my suggestions.
Meanwhile, keep it as two tests, unlike the suggestion by 'holder'.
The EXISTS would look something like
AND NOT EXISTS ( SELECT 1 FROM wp_postmeta
WHERE post_id = wp_posts.id
AND meta_key = 'visible_headlane'
AND meta_value = 'On' )
which would make good use of the PRIMARY KEY(post_id, meta_key) that I recommend.
(Is it really 'headlane', not 'headline'?)
Maybe something like this as jarlh suggested
SELECT
ID,post_title,post_content,comment_count,post_date,post_status,post_name
FROM wp_posts t1
left join (
SELECT DISTINCT post_id from wp_postmeta
WHERE meta_key IN ('visible_headlane','visible_homepage') AND meta_value = 'On' ) t2 on t1.ID = t2.post_id
WHERE post_type = 'post'
AND post_status = 'publish'
AND t2.post_id is null
ORDER BY post_date DESC
LIMIT 11, 32
I want to retrieve all the records that have a
meta_key = 'veteran_state'
where I pass in the state to return all the records of veterans from that state.
The wp_metapost has 3 meta_keys:
veteran_state
veteran_name
veteran_website
I have been working on the following query in MySQL:
SELECT *
FROM wp_postmeta
LEFT JOIN wp_posts ON wp_posts.id = wp_postmeta.post_id
WHERE wp_posts.post_type = 'veterans'
AND wp_postmeta.meta_value = 'colorado'
AND wp_posts.post_status = 'publish'
However, it returns only the veteran_state value.
I would like to return all the values for that state and render them to the browser as:
Veteran Website
John Doe http://website.com
Mary Veteran http://mywebsite.com
Thank you in advance for any help.
Try this: do the filtering on the individual table and then join the results.
SELECT A.* FROM
(SELECT * FROM wp_postmeta WHERE meta_value='colorado') A LEFT JOIN
(SELECT * FROM wp_posts WHERE post_type='veterans' AND post_status='publish') B
ON A.post_id=B.id;
I've have two sql queries which I'm trying to combine
The first:
SELECT * FROM wp_posts
JOIN wp_postmeta on (post_id=ID)
WHERE meta_key = "packageID" and meta_value = 1
ORDER BY post_date limit 50
Joins the wordpress wp_post table to the wp_postmeta and gets all the posts meeting with packageID = 1 (I think it might be an inelegant way of doing it but it works)
The second
SELECT * FROM wp_postmeta
JOIN wp_posts ON (meta_value=ID)
WHERE post_id = 2110
AND meta_key = '_thumbnail_id'
again joins the wp_post table to the wp_postmeta table, so for the post with the id 2110 it successfully gets the thumbnail for that posts. NB 2110 is just an example of an id
In Wordpress a thumbnail is a kind of post. So in this example the text which constitutes post 2110 is a associated with post 2115 - the latter being the thumbnail
What I'm trying to do is get the list as in the first query but also get thumbnails associated with each post
I think I need two joins but I can't see how to do it (being an sql beginner)
NB this will be in a script outside Wordpress so I can't use Wordpress's built-in functions
You can try this one,if there are more than one thumbnails for the post you can get the list of thumbnails separated by comma
SELECT
*,
(SELECT
GROUP_CONCAT(meta_value)
FROM
wp_postmeta
WHERE post_id = wp.ID
AND wpm.meta_key = "_thumbnail_id") AS `thumbnails`
FROM
wp_posts wp
JOIN wp_postmeta wpm
ON (wpm.post_id = wp.ID)
WHERE wpm.meta_key = "packageID"
AND wpm.meta_value = 1
ORDER BY wp.post_date
LIMIT 50
Note : GROUP_CONCAT has a limit to concat characters but you
can increase this limit
To get only one thumbnail you can try this
SELECT
*,
(SELECT
(meta_value)
FROM
wp_postmeta
WHERE post_id = wp.ID
AND wpm.meta_key = "_thumbnail_id" LIMIT 1)
FROM
wp_posts wp
JOIN wp_postmeta wpm
ON (wpm.post_id = wp.ID)
WHERE wpm.meta_key = "packageID"
AND wpm.meta_value = 1
ORDER BY wp.post_date
LIMIT 50
try with the following code
SELECT * FROM wp_posts wp JOIN wp_postmeta wm on (wp.post_id=wm.ID) WHERE wp.meta_key = "packageID" and wp.meta_value = 1 ORDER BY wp.post_date limit 50;
use proper alias and try it.
Try using the post_type column. The attachments have a post_type of 'attachment'. I can further explain if needed.
Also the post to which the thumbnail is attached to will be in the column post_parent.
global $wpdb;
$query7 = "SELECT distinct wp_postmeta.meta_value, wp_postmeta.meta_key, wp_posts.ID
FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_posts.post_status = 'publish'
AND wp_postmeta.meta_key = 'packageID'
AND wp_postmeta.meta_value = 1
AND (mt1.meta_key LIKE '_thumbnail_id')
$output = $wpdb->get_results( $query7 );
Use join with different aliases when joining same table more than once.
Hope this helps.
Try this
SELECT * FROM wp_posts P1
LEFT JOIN wp_postmeta M1 ON (M1.post_id=P1.ID)
WHERE (M1.meta_key = "packageID" and M1.meta_value = 1 )
LEFT JOIN wp_postmeta M2 ON (M2.meta_key=P1.ID AND M2.meta_key = '_thumbnail_id')
LEFT JOIN wp_posts P2 ON (M2.meta_value=P2.ID)
ORDER BY P1.post_date limit 50
I have an issue where I need to AS a column in a MySQL query and execute a BETWEEN on that column. The simplest version I can come up with that illustrates the problem is this:
SELECT ID AS post_id FROM wp_posts WHERE (post_id BETWEEN 10 AND 20)
This throws the following error:
#1054 - Unknown column 'post_id' in 'where clause'
Is there a way to make the newly created post_id column visible to the BETWEEN operator?
UPDATE:
This is my actual query. As you can see, it is slightly more complicated:
SELECT wp_postmeta.post_id,
MAX(CASE WHEN wp_postmeta.meta_key='store_lng' THEN wp_postmeta.meta_value END ) AS lng,
MAX(CASE WHEN wp_postmeta.meta_key='store_lat' THEN wp_postmeta.meta_value END ) AS lat
FROM wp_postmeta
LEFT JOIN wp_posts ON (wp_posts.ID=wp_postmeta.post_id)
WHERE
(lng BETWEEN 150.29793837662 AND 152.1161201948)
AND
(lat BETWEEN -34.775666623377 AND -32.957484805195)
AND
wp_posts.post_status='publish'
AND
wp_posts.post_type='store'
GROUP BY wp_postmeta.post_id
From the MySQL documentation:
It is not permissible to refer to a
column alias in a WHERE clause,
because the column value might not yet
be determined when the WHERE clause is
executed.
You'll either have to use ID instead of post_id in the WHERE clause or use a HAVING clause.
For example:
SELECT ID AS post_id FROM wp_posts WHERE ID BETWEEN 10 AND 20
Update: Based on your most recent update, you could use a subquery as already suggested or as I suggested earlier, you could use a HAVING clause.
For example:
SELECT wp_postmeta.post_id,
MAX(CASE WHEN wp_postmeta.meta_key = 'store_lng' THEN wp_postmeta.meta_value END) AS lng,
MAX(CASE WHEN wp_postmeta.meta_key = 'store_lat' THEN wp_postmeta.meta_value END) AS lat
FROM wp_postmeta
LEFT JOIN wp_posts
ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_status = 'publish' AND
wp_posts.post_type = 'store'
GROUP BY wp_postmeta.post_id
HAVING lng BETWEEN 150.29793837662 AND 152.1161201948 AND
lat BETWEEN -34.775666623377 AND -32.957484805195
Standard SQL disallows references to column aliases in a WHERE clause.
This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.
(http://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html)
Yes, you can!
SELECT post_id
from (select id AS post_id FROM wp_posts) x
WHERE post_id BETWEEN 10 AND 20
Edited to include changes to question. Try this (haven't run in mysql, but should be close if not correct):
SELECT *
FROM (SELECT
wp_postmeta.post_id,
MAX(CASE WHEN wp_postmeta.meta_key='store_lng' THEN wp_postmeta.meta_value END ) AS lng,
MAX(CASE WHEN wp_postmeta.meta_key='store_lat' THEN wp_postmeta.meta_value END ) AS lat
FROM wp_postmeta
GROUP BY wp_postmeta.post_id
) x
LEFT JOIN wp_posts ON wp_posts.ID = x.post_id
WHERE lng BETWEEN 150.29793837662 AND 152.1161201948
AND lat BETWEEN -34.775666623377 AND -32.957484805195
AND wp_posts.post_status='publish'
AND wp_posts.post_type='store'