I have used WPAllImport to import data from a CSV-file to Advanced Custom Fields.
I now want to put them back together with a SQL query, but dont know how to do it.
I've tried WPDataTables, but when I choose 5 or more tables, WPDataTables stops.
If I pick 2, I get this code
SELECT posts_podukter.post_title AS podukter_post_title,
podukter_meta_produkter_0_pris_tbl.meta_value AS podukter_meta_produkter_0_pris
FROM beta_h3L_posts AS posts_podukter
INNER JOIN (SELECT podukter_meta_produkter_0_pris_tbl_posts.ID as id, meta_value, meta_key FROM beta_h3L_postmeta AS podukter_meta_produkter_0_pris_tbl_postmeta INNER JOIN beta_h3L_posts AS podukter_meta_produkter_0_pris_tbl_posts ON podukter_meta_produkter_0_pris_tbl_postmeta.post_id = podukter_meta_produkter_0_pris_tbl_posts.ID AND podukter_meta_produkter_0_pris_tbl_posts.post_type = 'podukter') AS podukter_meta_produkter_0_pris_tbl
ON podukter_meta_produkter_0_pris_tbl.meta_key = 'produkter_0_pris' AND podukter_meta_produkter_0_pris_tbl.id = posts_podukter.ID
WHERE 1=1
AND posts_podukter.post_type = 'podukter'
I think this is too much code.
Can someone help me to get on the right way.... :-)
This is what the table should look like
Here is a capture how the table should look like
I would agree that this is "too much code" which sounds sort of ridiculous, but in this case totally applies. That SQL statement that was produced could be written as:
SELECT
post.post_title as podukter_post_title,
postmeta.meta_value as podukter_meta_produkter_0_pris
FROM beta_h3L_posts AS posts
INNER JOIN beta_h3L_postmeta AS postmeta
ON postmeta.post_id = post.ID
AND postmeta.meta_key = 'produkter_0_pris'
WHERE posts.post_type = 'podukter'
If there is another metavalue that you need you can join again to your meta table:
SELECT
post.post_title as podukter_post_title,
postmeta.meta_value as podukter_meta_produkter_0_pris,
postmeta2.meta_value as tilbudspris
FROM beta_h3L_posts AS posts
INNER JOIN beta_h3L_postmeta AS postmeta
ON postmeta.post_id = post.ID
AND postmeta.meta_key = 'produkter_0_pris'
INNER JOIN beta_h3L_postmeta AS postmeta2
ON postmeta.post_id = post.ID
AND postmeta2.meta_key = 'tilbudspris'
WHERE posts.post_type = 'podukter'
I don't know what any of these words mean (besides post and postmeta) so I'm just going to assume that this is right/helpful.
The only thing is that you may want to switch to using a LEFT OUTER JOIN to your postmeta table just in case the meta_key you are after doesn't exist for the post.id you are querying. In that case, with an INNER JOIN the id/post will be dropped from the result set where a LEFT OUTER JOIN will show the id/post record with a blank for whatever that corresponding meta_value is that you are joining in.
Related
How can i simply return a left join's column values as a separate key in a query?
SELECT meta.*
, post.* as venue
, venue_meta.* as venue_meta
FROM $wpdb->postmeta as meta
LEFT
JOIN $wpdb->posts as post
ON meta.meta_key = '_EventVenueID'
AND meta.meta_value = post.id
LEFT
JOIN $wpdb->postmeta as venue_meta
ON venue_meta.post_id = venue.id
WHERE meta.post_id = %d
GROUP
BY meta.meta_id
So i'm expecting all the columns in the post table to return as $response['venue'], but I have a syntax error on the first line.
Basically, a construct like post.* as venue isn't valid and produces the syntax error.
You can use one alias for one column name like post.id as id
But you will never get nested results for joins. The select will retrieve rows, which consist of a vector of columns with their values.
The short form of your query then would be
SELECT *
FROM $wpdb->postmeta as meta
LEFT
JOIN $wpdb->posts as post
ON meta.meta_key = '_EventVenueID'
AND meta.meta_value = post.id
LEFT
JOIN $wpdb->postmeta as venue_meta
ON venue_meta.post_id = venue.id
WHERE meta.post_id = %d
which would give you all the columns from all tables in one row (or array in PHP).
To split that into sub-arrays, the result-set let's you iterate over the result-sets properties like columns names and table names. This might help to build up the sub-arrays programmatically.
I have a table displaying on my website with a list of projects. The SQL statement below pulls in each project and converts the ###_id columns to the ###_name in another table. So far so good.
The problem I have is that this is requiring all fields in a row in the projects table to be filled out. If, for example, the project row has no value for 'proj_industry_id' then the project won't display here at all.
I've tried removing the 'AND' for each match-up in the WHERE statement and separating them with commas, but it errors out.
I've also checked SQL docs and can't seem to find my way to an answer over there.
Any ideas on how I can get my statement to still match up the id with the name when I have one, but still show the record when I don't?
Thanks!
$sql = "SELECT
projects.*,
engagement_types.eng_type_name AS eng_type,
users.user_full_name AS username,
industries.industry_name AS industry_name,
categories.category_name AS category_name,
geographies.geo_name AS geo_name,
status.status_name AS status_name
FROM
projects,
engagement_types,
users,
industries,
categories,
geographies,
status
WHERE
projects.proj_eng_type_id = engagement_types.id
AND projects.proj_lead_id = users.id
AND projects.proj_industry_id = industries.id
AND projects.proj_category_id = categories.id
AND projects.proj_geo_id = geographies.id
AND projects.proj_status_id = status.id
AND projects.proj_geo_id = '$selected_geo_id'";
*****EDIT******
Here is the final correct code from the solution below using multiple left joins!
SELECT
projects.*,
engagement_types.eng_type_name AS eng_type,
users.user_full_name AS username,
industries.industry_name AS industry_name,
categories.category_name AS category_name,
geographies.geo_name AS geo_name,
status.status_name AS status_name
FROM
projects
LEFT JOIN engagement_types ON projects.proj_eng_type_id = engagement_types.id
LEFT JOIN users ON projects.proj_lead_id = users.id
LEFT JOIN industries ON projects.proj_industry_id = industries.id
LEFT JOIN categories ON projects.proj_category_id = categories.id
LEFT JOIN geographies ON projects.proj_geo_id = geographies.id
LEFT JOIN status ON projects.proj_status_id = status.id
GROUP BY
proj_start_date
sounds like your have to look at "LEFT JOIN" https://www.w3schools.com/sql/sql_join_left.asp
Otherwise you miss the left part of the green circle.
i am writing the following SQL statement using my existing wordpress db as a learning environment...
Select
wp.ID,
wpum.user_id,
wpp.post_name
From
wp_users As wp Left Join
wp_usermeta As wpum
On wp.ID = wpum.user_id Left Join
wp_posts As wpp
On wpum.user_id = wpp.post_author
Where
wpp.post_status = 'publish' and
wpum.meta_key = 'nickname' and
wpp.post_name not in ('sample%', 'hello%')
order by
wpp.ID desc
As you can see I have used alias's for my tables and filtered my results of the word press posts to be only 'published' posts and only selecting the nickname to avoid duplicate results by user_id.
My issue is the wildcards do not appear to do anything.. my results are as returned with the sample & header posts still showing.
However, this would work as:
Select
wp.ID,
wpum.user_id,
wpp.post_name
From
wp_users As wp Left Join
wp_usermeta As wpum
On wp.ID = wpum.user_id Left Join
wp_posts As wpp
On wpum.user_id = wpp.post_author
Where
wpp.post_status = 'publish' and
wpum.meta_key = 'nickname' and
wpp.post_name not like 'sample%' and
wpp.post_name not like 'hello%'
order by
wpp.ID desc
My output is with the desired results removed.. anybody able to help me out? The second example is perfectly usable... I would just like to know my alternatives to learn good practices.
I've tried googling and using a few examples, but not having much luck.
Thanks all
Apol's if my post is wrongly formatted or this has been answered before, again I have searched!
Yeah, little confused here. I have number of tables in one of my wordpress installations and the name of them and the field names are pretty much descriptive. So I don't think that you need any explanation for them. My problem here is actually with the forth inner join I am trying to do. The query is simple:
SELECT *
FROM wp_posts AS post
INNER JOIN wp_postmeta AS postmeta ON post.ID = postmeta.post_id
INNER JOIN wp_game AS game ON post.ID = game.post_id
INNER JOIN wp_game_platform AS platform ON game.game_id = platform.GameId
INNER JOIN wp_platform_release AS release ON release.PlatformID = platform.ID
WHERE post.post_type = 'games' AND postmeta.meta_key = 'post_views_count' AND platform.Status = 0 AND release.ReleaseDate > 1435917562
ORDER BY postmeta.meta_value DESC, game.game_id
LIMIT 100
This is supposed to get list of all the game platforms (Xbox version, PC version of a game) that are not released yet and sort them with their page visits. As you can see the page visits are stored as post meta so I did a join on wp_postmeta. The above query gives me this syntax error:
failed : You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'release ON release.PlatformID = platform.ID WHERE post.post_type
= 'games' AND ' at line 1
Which clearly indicated that there is a syntax error in the query that I can't see. Funny enough if I run the following query, it returns the expected results (all the games [released or not] sorted by their post visits):
SELECT *
FROM wp_posts AS post
INNER JOIN wp_postmeta AS postmeta ON post.ID = postmeta.post_id
INNER JOIN wp_game AS game ON post.ID = game.post_id
INNER JOIN wp_game_platform AS platform ON game.game_id = platform.GameId
WHERE post.post_type = 'games' AND postmeta.meta_key = 'post_views_count' AND platform.Status = 0
ORDER BY postmeta.meta_value DESC, game.game_id
LIMIT 100
At first I thought that there is a JOIN limit but after research I found out that the limit is more than 60 tables which is far from 5. So you see any thing that I can't?
Thanks
release is a reserved word - try changing the name of the alias to something else. See : https://dev.mysql.com/doc/refman/5.0/en/keywords.html
In the home page of a website I need to return the last 3 posts of the blog, created with wordpress.
I have this code:
SELECT p.post_title, p.post_date, p.post_content, wpr.object_id, dt_blog_terms.name, dt_blog_terms.slug
FROM dt_blog_terms
INNER JOIN dt_blog_term_taxonomy ON dt_blog_terms.term_id = dt_blog_term_taxonomy.term_id
INNER JOIN dt_blog_term_relationships wpr ON wpr.term_taxonomy_id = dt_blog_term_taxonomy.term_taxonomy_id
INNER JOIN dt_blog_posts p ON p.ID = wpr.object_id
WHERE taxonomy = 'category'
AND p.post_type = 'post'
AND p.post_status = 'publish'
AND slug != 'notizie-notifiche'
ORDER BY `post_date` DESC
LIMIT 3
As you can see, I have one cathegory (notizie-notifiche) that I want to exclude. This sql string works, but I have a problem when the post has more than one cathegory. In this case, it is returned once for every cathegory, while I want to show it only once in total.
Any idea?
I thought I could use DISTINCT, but it does not seem to work with this kind of SELECT statement.
I can tell you what you need to do, but I can't modify the query to do it. You use the term "category" in your question, but there is no field by that name in your data.
You need to aggregate your data, at the level you want, and then include a having clause. The following group by may solve your problem:
group by p.id
having sum(case when slug = 'notizie-notifiche' then 1 else 0 end) = 0