Hi i am trying to order my database by comma separated value but it doesn't seem to be working correctly for me. here is my mysql query.
SELECT *
FROM wp_posts
LEFT JOIN wp_postmeta ON wp_posts.ID=wp_postmeta.post_id
LEFT JOIN wp_term_relationships ON wp_posts.ID=wp_term_relationships.object_id
LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id=wp_term_taxonomy.term_id
LEFT JOIN wp_terms ON wp_term_taxonomy.term_id=wp_terms.term_id
WHERE wp_terms.name = 'Dimmers'
AND meta_key = 'feature_number_of_channels'
GROUP BY wp_posts.ID
ORDER BY meta_value ASC
And here is a screenshot.
As you can see from the screenshot they dont seem to be working correctly any help please on how to order these ASC correctly???
Correct order i am looking for is so the highest out of them all then ASC like this
24,48 - 4,6,12,24 - 12 - 6 - 6 - 3
Change your ORDER BY to look at the integer value of the string :
ORDER BY CONVERT(SUBSTRING_INDEX(meta_value, ',', -1), SIGNED) DESC
Here's an example:
SELECT "24,38" AS meta_value UNION
SELECT 1 AS meta_value UNION
SELECT 30 AS meta_value
ORDER BY CONVERT(SUBSTRING_INDEX(meta_value, ',', -1), SIGNED) DESC
returns:
Rows = 3
meta_value
24,38
30
3
Good luck!
Using order table.field ASC might help you as there are too many tables joined. That way the server would take the exact order from one single table.
Related
I am trying to find duplicates of wordpress posts using SQL - but duplicates according to duplicate post meta - not duplicate titles. So far the closest code I could find does the opposite - it finds all the unique posts. How can I reverse this query?
SELECT id,meta_value, post_title, post_content
FROM wp_posts
LEFT JOIN wp_postmeta c ON ( wp_posts.ID = c.post_id )
WHERE post_type = 'post' AND meta_key = 'syndication_permalink'
GROUP BY meta_value
HAVING Count(meta_value) > 1
*UPDATE sorry for being a noob at SQL.. I have added a table to show exactly what the aim is. I want to delete the duplicate posts from freelancer.com
post_id meta_key meta_value
-------- ------------------- ----------------------------------------
1 syndication_permalink https://www.freelancer.com/projects/
2 syndication_permalink https://www.freelancer.com/projects/
3 syndication_permalink https://www.freelancer.com/projects/
4 syndication_permalink https://www.simplyhired.com/job/W6sVJ1
5 syndication_permalink https://www.mandy.com/uk/job/576913/junior
I am not Sure, What you want as your Output. But Try this code and let me know whether it solved your problem or not.
SELECT x.*
FROM wp_posts x
JOIN
(SELECT wp.meta_value
FROM wp_posts wp
LEFT JOIN wp_postmeta c ON ( wp.ID = c.post_id )
WHERE post_type = 'post' AND meta_key = 'syndication_permalink'
GROUP BY wp.meta_value
HAVING Count(wp.meta_value) > 1) as y ON y.meta_value = x.meta_value
This will give all duplicate value as per column meta_value. Let me correct if I am Wrong.
I'm trying to fetch rows, ordered by ID ASC but I also want that result to be randomized afterwards. So basically, if I want to fetch the first 15 rows, I want them to come out randomized but as the first 15 rows.
Basically, I have a frame that loads the first 15 clients, and a button "load more" to load 15 more clients. I simply want the same 15 clients to come out by order ID, but have their positions randomized.
This is what I have thus far, but the RAND() at the end is not having any impact:
SELECT wp_posts.ID, wp_posts.post_title,
wp_postmeta.meta_value,
axess_clients.client_nom,
axess_clients.site
FROM wp_posts
LEFT JOIN axess_clients ON axess_clients.client_id = wp_posts.ID
LEFT JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_type='clients' AND wp_posts.post_status='publish'
AND wp_postmeta.meta_key='_thumbnail_id'
ORDER BY wp_posts.ID ASC, RAND() LIMIT 15 OFFSET ".$_POST['data']
Is there a way to do this via MySQL or do I really have to pull out PHP for this?
When you have multiple expressions in ORDER BY, the second expression is used to order within a group where the first expression is equal. It can't reorder rows that are already ordered by the first expression.
To reorder something, you need to put the first ordering in a subquery.
SELECT *
FROM (
SELECT wp_posts.ID, wp_posts.post_title,
wp_postmeta.meta_value,
axess_clients.client_nom,
axess_clients.site
FROM wp_posts
LEFT JOIN axess_clients ON axess_clients.client_id = wp_posts.ID
LEFT JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_type='clients' AND wp_posts.post_status='publish'
AND wp_postmeta.meta_key='_thumbnail_id'
ORDER BY wp_posts.ID ASC
LIMIT 15 OFFSET " . $_POST['data']) AS x
ORDER BY RAND()
I have a table wp_views, with columns postid and views
I want to get the IDs that have the highest values of views (top 4)
Then return the title and link from wp_posts using the postid.
What's the right way of doing this?
You can try the following
global $wpdb;
$top4=$wpdb->get_results('SELECT post_title, post_name from `'.$wpdb->prefix.'views`
INNER JOIN `'.$wpdb->prefix.'posts` ON `postid`=`ID`
ORDER BY `views` DESC
LIMIT 4;', ARRAY_A);
I have tried to replicate your table structure from what your write and from this i have come up with the following:
SELECT id, title, link
FROM wp_views RIGHT JOIN wp_posts ON wp_posts.id = wp_views.post_id
ORDER BY views DESC
LIMIT 4;
you can try it out here: http://sqlfiddle.com/#!9/1cea23/1
I am using RIGHT JOIN to allow null values in the wp_posts part of the result. If you want to avoid NULL values in your results you can use INNER JOIN instead.
I have a meta key which is set by a select drop down so a user can select an option between 1 and 14 and then save their post. I want the posts to display on the page from 1 to 14 ordered by date but if the user creates a new set of posts the next day I also want this to happen so you have posts 1 to 14 each day displaying in that order.. the SQL i have so far is as follows
SELECT SQL_CALC_FOUND_ROWS
wp_postmeta.meta_key,
wp_postmeta.meta_value,
wp_posts.*
FROM wp_posts
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE 1=1
AND wp_posts.post_type = 'projectgallery'
AND ( wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private')
AND (wp_postmeta.meta_key = 'gallery_area' )
GROUP BY wp_posts.post_date asc
ORDER BY CAST(wp_postmeta.meta_value AS UNSIGNED) DESC,
DATE(wp_posts.post_date) desc;
Which gives me the following output noticte thatthe posts entered at different dates with either 1 or 3 show up in sequence, ideally i want the latest ones to display directly after 14 so it starts over again. the number 14 should not be static either as if someone adds another option to the select then it will increase and decrease if an option is removed.
GROUP BY is confusingly named. It only makes sense when there's a SUM() or COUNT() or some such function in the SELECT clause. It's not useful here.
The canonical way of getting a post_meta.value into a result set of post items is this. You're close but this makes it easier to read.
SELECT SQL_CALC_FOUND_ROWS
ga.meta_value gallery_area,
p.*
FROM wp_posts p
LEFT JOIN wp_postmeta ga ON p.ID = ga.post_id AND ga.meta_key = 'gallery_area'
WHERE 1=1
AND p.post_status IN ('publish', 'private')
AND p.post_type = 'projectgallery'
Notice the two parts of the ON clause in the JOIN. That way of doing the SQL gets you just the meta_key value you want cleanly.
So, that's your result set. You'll get a row for every post. If the metadata is missing, you'll get a NULL value for gallery_area.
Then you have to order the result set the way you want. First order by date, then order by gallery_area, like so:
ORDER BY DATE(p.post_date) DESC,
0+gallery_area ASC
The 0+value trick is sql shorthand for casting the value as an integer.
Edit. Things can get fouled up if the meta_value items contain extraneous characters like leading spaces. Try diagnosing with these changes. Put
DATE(p.post_date) pdate,
0+ga.meta_value numga,
ga.meta_value gallery_area
in your SELECT clause. If some of the numga items come up zero, this is your problem.
Also try
ORDER BY DATE(p.post_date) DESC,
0+TRIM(gallery_area) ASC
in an attempt to get rid of the spaces. But they might not be spaces.
alright so i've got two tables here, gforum_Post and gforum_PostView
i'm trying to get information from the table gforum_Post where it has the same post_id.
"SELECT post_subject, post_username FROM `gforum_Post` WHERE post_id = 341"
i've got it to display what i want but it's hard coded, the data in the columns will change. here's what i've got so far
SELECT post_id_fk, post_thread_views FROM `gforum_PostView` ORDER BY `gforum_PostView`.`post_thread_views` DESC
SELECT post_subject, post_username FROM `gforum_Post` WHERE post_id = 341
341 is the value that is hardcoded
You can use join to join the tables:
SELECT post_id_fk, post_thread_views, post_subject, post_username FROM `gforum_PostView`
join `gforum_Post` on post_id =post_id_fk
ORDER BY `gforum_PostView`.`post_thread_views` DESC
I think what you're looking for is the JOIN query:
SELECT GPV.post_id_fk, GPV.post_thread_views, GP.post_subject, GP.post_username
FROM gforum_Post GP, gforum_PostView GPV
WHERE GP.post_id = 341
ORDER BY GPV.post_thread_views DESC