I wonder if there is a way of merging these two database queries so as to have one:
$result = $wpdb->get_row("SELECT meta_value FROM ".$wpdb->prefix.
"postmeta WHERE meta_key = '_cat_num' AND post_id = $var");
$name = $wpdb->get_row("SELECT name FROM ".AH_FEED_DETAILS_TABLE.
" WHERE id = " . (int)$result->meta_value);
return $name->name;
The first query finds the category value which is then used to find the name field in the AH_FEED_DETAILS_TABLE table
You can get the database to do the work for this by using the IN with a sub-query as follows:
$name = $wpdb->get_row("SELECT name FROM ".AH_FEED_DETAILS_TABLE.
" WHERE id IN (SELECT meta_value FROM ".$wpdb->prefix.
"postmeta WHERE meta_key = '_cat_num' AND post_id = $var)");
$result = $wpdb->get_row("SELECT name FROM ".$wpdb->prefix
.AH_FEED_DETAILS_TABLE." AS cat_tbl JOIN postmeta ON
cat_tbl.id=postmeta.meta_value
WHERE postmeta.meta_key = '_cat_num' AND postmeta.post_id = $var");
return $result->name;
I hope that helps giving the idea
Related
postmeta table example
The end result is matching an array of 'post_id', sorted by the number of attendees. I know it is a WP backend but I have to do it in SQL, no 'get_posts' or anything WordPress related. What I'm wanting to do is confusing so I'll try to be clear.
What I Have To Begin:
$check_post_ids - the original array of post_ids I need to check
$start_date - the 'meta_value' each event's '_StartDate' needs to match.
What I Need To Do:
I need to check these three post_ids to see if they have a matching start date. If they do, I need to get an array of post_ids sorted from the highest to lowest number of attendees.
Currently I was planning on doing this with multiple SELECTs and foreach() statements, but I feel like there has to be a simpler way to do this ( i.e. One SELECT & foreach() ). Here's what I'm doing at the moment. I haven't finished it yet because I feel like there has to be a simpler way to do this. Newer to SQL and any help is tremendously appreciated!
$check_post_ids = array('484', '627', '982', '2435');
$start_date = '1963-10-20 19:30:00';
// iterate through array of post_ids and check if they have the same _StartDate
foreach($check_post_ids as $id){
$start_date_check = "SELECT * FROM `wp_postmeta` WHERE `post_id` =" . $id . " AND `meta_key` LIKE '_StartDate' AND `meta_value` = '" . $start_date . "'";
$start_date_check_result = mysqli_query($conn, $start_date_check);
// assign all post_ids with a matching _StartTime to a new array
if (mysqli_num_rows($start_date_check_result) > 0) {
while($row = mysqli_fetch_assoc($start_date_check_result)) {
$matching_post_ids[] = $row['post_id'];
// iterate through matching_post_ids array, get the _NumAttendees for each, and assign their id and _NumAttendees to an assoc array to be sorted
foreach($matching_post_ids as $id){
$attendees_check = "SELECT meta_value FROM wp_postmeta WHERE post_id = " . $id . " AND meta_key = '_ecp_custom_2'";
$attendees_check_result = mysqli_query($conn, $attendees_check);
if($upvotes_check > 0){
while($row = mysqli_fetch_assoc($attendees_check_result)){
$sort_by_attendees['id'] = $id;
$sort_by_attendees['attendees'] = $row['meta_value'];
$sort_by_attendees_array[] = $sort_by_attendees;
}
}
}
For the first part of the query, I think you can simplify your code by using SQL IN keyword. Basically it substitutes the role of your first array with all your post IDs. Then, you can write a SQL query like this :
SELECT meta_value
FROM wp_postmeta
WHERE meta_key = '_ecp_custom_2'
AND post_id IN (SELECT post_id
FROM wp_postmeta
WHERE post_id IN ('484', '627', '982', '2435')
AND meta_key LIKE '_StartDate'
AND meta_value = '" . $start_date . "'")
ORDER BY meta_value DESC
There are 2 queries. The first one in the parenthesis, subselect all the post ids on your table wp_postmeta where post_ids have ids in your list and if they match with your starting date. Then, the main query selects all meta_values (I suppose attendees) based on your first subquery (all post ids with your starting date).
Hope it will help you.
I have a database with several tables. I'm able to query IDs from a single table. What I'd like to do is Use those IDs to query another tables IDs, then use these new IDs to query fields from the final table. Here is what I am currently doing:
Here is how I acquire the first set of IDs:
$returnedPost = mysqli_query($con, "SELECT Region_ID FROM Region WHERE RegionName='" . $queryVar . "'");
function resultToArray($result) {
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
$rows = resultToArray($returnedPost);
//$rows[x]['Region_ID'];//returns Region_ID 1...n
I'd like to use the IDs in $rows to be able to query a new set of IDs from other tables as follows:
$newTbl = mysqli_query($con, "SELECT Location_ID FROM Location WHERE Region_ID=" . $rows[$x]['Region_ID']);
$rows2 = resultToArray($newTbl);
$finalTbl = mysqli_query($con, "SELECT Field1, Field2 FROM Posts WHERE Location_ID=" . $rows2[$x]['Location_ID']);
Can someone please tell me how I can accomplish this? Thanks.
you can use INNER JOIN in one query to get at this data, maybe something like this
SELECT P.Field1,P.Field2
FROM Region R
INNER JOIN Location L ON R.Region_ID = L.Region_ID
INNER JOIN Posts P ON L.Location_ID = P.Location_ID
WHERE R.RegionName = Your_Region_QueryVar
I am trying to pull data from my database which ahs the following:
x3 tables:
docs
doc_id - index
cat
cat_id - index
doc_cat_join
doc_id - foreign key to docs/doc_id
cat_id - foreign key to cats/cat_id
I have inserted some data so that I have a row in cats and a rows in docs and in the join table bound a cat_id with a doc_id which all works fine. I am tying to pull that from the tables and show it, here is my approach so far but not getting anything out and am wondering where my failings are as I am getting no errors?
when I visit the cateroy page in my scripts I GET the id from the url:
$id = $_GET['cat_id'];
$q = sprintf("
SELECT
docs.doc_id,doc_name
FROM docs
INNER JOIN doc_cat_join
ON cats.cat_id = doc_cat_join.cat_id
WHERE doc_cat_join.doc_id = '%s'
",
mysqli_real_escape_string($dbc, $id) );
$r = mysqli_query($dbc,$q)
or die ("Couldn't execute query: ".mysqli_error($dbc));
// FETCH AND PRINT ALL THE RECORDS
echo '<div class="view_body">';
while ($row = mysqli_fetch_array($r)) {
echo ' '.$row["doc_name"]. '<br>';
}
echo '</div>';
}
for some bizarre reason I get nothing, if I do a var_dump on $r I get the following:
string(168) " SELECT docs.doc_id,doc_name FROM docs INNER JOIN doc_cat_join ON cats.cat_id = doc_cat_join.cat_id WHERE doc_cat_join.doc_id = '24' "
So it gets the correct category ID I am in.
I am now getting th following output:
Couldn't execute query: Unknown column 'cats.cat_id' in 'on clause'
as per your input and query design, your query should be as per below-
SELECT d.doc_id, d.doc_name
FROM docs AS d
INNER JOIN doc_cat_join AS dc ON dc.doc_id = d.doc_id WHERE dc.cat_id = '24';
Note: If it is not then show what output you require.
$id = $_GET['cat_id'];
$q = sprintf("
SELECT
*
FROM docs
INNER JOIN doc_cat_join
ON docs.doc_id = doc_cat_join.doc_id
INNER JOIN cat
ON doc_cat_join.cat_id = cat.cat_id
WHERE doc_cat_join.cat_id = '$id'
",
mysqli_real_escape_string($dbc, $id) );
print_r ($q);
// FETCH AND PRINT ALL THE RECORDS
echo '<div class="view_body">';
while ($row = mysqli_fetch_array($r)) {
echo ' '.$row["doc_name"]. '<br>';
}
echo '</div>';
}
your query is wrong I think.
I would like to get all the the corresponding images from one mySQL table that have a corresponding post_ID to the ID of another table.
So with an ID of 7 in table wp_post, I want to echo the values of infomation in another table that has a certain meta_key and a corresponding ID.
===WP_POST=========== ===WP_POSTMETA======================================
ID || content post_id || meta_key || meta_value
================================================================================
The meta_key that I want is listed as _wp_attached_file in the database.
So I want to get all meta_values of wp_postmeta table that have a meta_key of _wp_attached_file and a post_id that matches the current ID of the page being viewed from the table wp_posts.
So obviously the tables will need to be joined but I need a hand.
Any ideas,
Marvellous AHAA
Implemented solution below and I am now receiving an error.
<?php
$pics = mysql_query("SELECT t2.meta_key,t2.meta_value from (select id from wp_post) t1
join wp_postmeta t2 on (t2.post_id=t1.postid)
where t2.meta_key='_wp_attached_file'");
while ($row = mysql_fetch_array($pics)) {
$thumb = $row['meta_value'];
echo '<img src="http://www.golfbrowser.com/wp-content/uploads/'.$thumb.'" />';
}
?>
Any ideas?
I tried a simpler way myself. Still does not work.Can anyone see what is wrong.
<?php
$post_id = $post->ID;
$pics = mysql_query("
SELECT meta_value * FROM wp_postdata
WHERE meta_key ='_wp_attached_file'
AND post_id = '%$post_id%'
");
while ($row = mysql_fetch_array($pics)) {
$thumb = $row['meta_value'];
echo '<img src="http://www.golfbrowser.com/wp-content/uploads/'.$thumb.'" />';
}
?>
Try this query:
SELECT A.meta_key
FROM WP_POSTMETA AS A
JOIN WP_POST AS B
ON (A.post_id = B.ID)
WHERE A.post_id='%post_id%' ;
I have this query:
$query = "SELECT *FROM wp_postmeta WHERE meta_key = '_isEvent' AND meta_value ='yes' ORDER BY post_id LIMIT 0, 5" or die(mysql_error());
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$eventid = $row ['post_id'];
echo "<p>".$eventid."</p>";
}
This currently posts some ids $eventid. I now want to run another query in another table (same database) that pulls some post titles that matches those id's.
Table is called "wp_posts" and column to match is "ID" and want to echo out post title from "post_title".
Where do I start?
Sounds like you want to use SQL joins. W3 schools has a good intro article about this.
http://www.w3schools.com/sql/sql_join.asp
Something like the following
SELECT post_title FROM wp_postmeta m, wp_posts p WHERE m.wmeta_key = '_isEvent' AND m.meta_value ='yes' AND m.post_id == p.post_idORDER BY m.post_id LIMIT 0, 5