Select where class from wordpress database: - php

I need help to this select where class from wordpress database:
What I would like to achieve is to display the first 6 newcomer posts/records.
The path to the image, image name, property title, property features (How many Bedrooms and Bathroom etc. there is)
property price and property price unit (if it $ or Danish kroner etc.)
At this link All properties here you can see what i will like to achieve on my front page, just without google map and the sort list and only the 6 first post:
All properties
This are tables: wp_wpl_properties, wp_wpl_units, wp_wpl_items to achieve my goal so i tried to make this select query:
enter code here
<?php
global $wpdb;
$results = $wpdb->get_results
("SELECT * FROM wp_wpl_properties,wp_wpl_units,wp_wpl_items
where wp_wpl_properties.price_unit= wp_wpl_units.id and wp_wpl_properties.id= wp_wpl_items.parent_id LIMIT 6;");
foreach ( $results as $result ) {
?>
I have attached table files to this question, here:
wp_wpl_units.pdf
wp_wpl_items.pdf
wp_wpl_properties (1).sql
The code I've made does not make any errors.
My problem is that i get the same record displayed 3 times at the fist columns and the same at the next 3 column, hope this make sens :)
Here is a link to my frontpage: My frontpage

Do you want like this?
$results = $wpdb->get_results ("
SELECT * FROM wp_wpl_properties
join wp_wpl_units on wp_wpl_properties.living_area_unit= wp_wpl_units.id
join wp_wpl_properties on wp_wpl_properties.id= wp_wpl_items.parent_id LIMIT 6");
or
$results = $wpdb->get_results ("
SELECT * FROM wp_wpl_properties
join wp_wpl_units on wp_wpl_properties.living_area_unit= wp_wpl_units.id
join wp_wpl_properties on wp_wpl_properties.id= wp_wpl_items.parent_id
where wp_wpl_properties.id= 1");

$results = $wpdb->get_results
("SELECT * FROM wp_wpl_properties
Inner Join wp_wpl_units ON wp_wpl_properties.living_area_unit = wp_wpl_units.id
Inner Join wp_wpl_items ON wp_wpl_properties.id= wp_wpl_items.parent_id
LIMIT 6;");

In your MySQL query,
SELECT * FROM wp_wpl_properties,
wp_wpl_units,
wp_wpl_items
where wp_wpl_properties.price_unit=wp_wpl_units.id
and wp_wpl_properties.id= wp_wpl_items.parent_id
LIMIT 6
MySQL individually checks each of the three columns differently and generates output for all of them even though they are same.
Doing an inner join instead of checking individually on three columns would be the solution. Like,
SELECT DISTINCT * FROM wp_wpl_properties as props
Inner Join wp_wpl_items as items ON items.parent_id = props.id
Inner Join wp_wpl_units as units ON units.id = props.living_area_unit
LIMIT 6
And instead of select distinct * from, get only the columns you need. it will speed up the query to a large extent!
like SELECT DISTINCT props.ID, items.ID FROM ....
Hope this helps.

Related

Selecting from comments and images table for each article in a while loop

I have an articles table that and I am displaying it in the homepage in a while loop. Inside the while loop I want to display the comments count and images count for each article.
It is working for me now, but it is three queries in total, I am trying to combine it in the first query and then just display all of them in one while loop. Here is what I am trying to achieve:Articles page
The current format I am following:
//a represents articles table, c represents comments table, i represents image table
$query = mysqli_query($conn, "SELECT a.a_id, a.title, a.datetime, a.user_id FROM a ORDER BY a.datetime DESC");
while($fetch = mysqli_fetch_assoc($query){
$imageQ = msqli_query($conn, "SELECT COUNT(image_path), image_path FROM i WHERE a_id = '$fetch['a_id']'");
$imageFetch = mysqli_fetch_assoc($imageQ);
$commentQ = mysqli_query($conn, "SELECT COUNT(comment_id) FROM c WHERE a_id = '$fetch['a_id']'");
$commentFetch = mysqli_fetch_assoc($commentQ);
}
I want to cram all of these queries into one single query that fetches the article and comments count and image count for each article and the first image.
The images and comments are separate dimensions of the data. So, you have to be careful about how to bring them together. In your case, you can aggregate the values before doing the joins:
SELECT a.a_id, a.title, a.datetime, a.user_id,
i.num_images, c.num_comments
FROM a LEFT JOIN
(SELECT a_id, COUNT(image_path) as num_images
FROM i
GROUP BY a_id
) i
ON i.a_id = a.a_id LEFT JOIN
(SELECT a_id, COUNT(comment_id) as num_comments
FROM c
GROUP BY a_id
) c
ON c.a_id = a.a_id
ORDER BY a.datetime DESC;
You can use mysql nested queries something like
SELECT a.,tab1.,tab2.* FROM a INNER JOIN (SELECT * FROM b ) as tab1 INNER JOIN (SELECT * FROM c) as tab2
Hope this can get you to get desired output.
Thanks

Select data from two tables with speed

I have two tables that I want to select from. I have a table called articles which stores various articles for my content management system and I have another called articles_meta_data that stores meta data for the articles table. In order to get an article's meta data, you select from articles_meta_data using the article Id. My code is here and works perfectly. Is there a way I can optimize the code and make it faster?
$result = mysql_query("SELECT * FROM articles");
while ($row = mysql_fetch_object($result)) {
$result2 = mysql_query("SELECT * FROM articles_meta_data WHERE article_ID=" . $row->ID);
while ($row2 = mysql_fetch_object($result2)) {
var_dump($row2);
}
}
join the tables so you will only query the database once, eg
SELECT b.*
FROM articles a
INNER JOIN articles_meta_data b
ON a.ID = b.article_ID
To further lean more about join, please see the link below
Visual Representation of SQL Joins
you should use join query so that you dont need to fire two queries, but first you have to decide which join to use for this check this link http://www.firebirdfaq.org/faq93/
here we will use JOIN so that records with same value for columns articles.ID and articles_meta_data.article_ID will be selected
SELECT *,articles_meta_data.field1,articles_meta_data.field2 from articles JOIN articles_meta_data ON articles.ID = articles_meta_data.article_ID
You can also do something like this..
Select * from articles JOIN articles_meta_data ON ID = article_ID
Hope this may also help you in some other way.

get count of posts based on count(*)

i am trying to get number of posts that i have
Here is my query
$Query="
SELECT t.*,u.*,c.*
FROM posts as t
LEFT JOIN relations as r on r.post_id = t.post_id
LEFT JOIN users as u on t.auther_id = u.auther_id
LEFT JOIN categories as c on c.cate_id = r.cate_id
GROUP BY t.post_id
";
$Query=mysql_query($Query);
$numberOfPosts=mysql_num_rows($Query);
This query is works very well
but i am trying to convert it, i want make it faster
i want use count(*) instead of t.*
because when i use t.*, it gets the full data of posts and categories
but i want to get count only, So i decided to use count(*) but i don't know how to use it with query like this
Edit
i've replaced SELECT t.*,u.*,c.* with SELECT count(t.*)
But i got mysql Error Warning: mysql_fetch_assoc(): supplied argument
Edit 2:
i am trying SELECT count(t.post_title)
I Got this results
Array ( [count(t.post_id)] => 10 )
But i have only 2 posts!
$Query="
SELECT t.*,u.*,c.*
FROM posts as t
LEFT JOIN relations as r on r.post_id = t.post_id
LEFT JOIN users as u on t.auther_id = u.auther_id
LEFT JOIN categories as c on c.cate_id = r.cate_id
GROUP BY t.post_id
";
$Query=mysql_query($Query);
$numberOfPosts=mysql_num_rows($Query);
Let's take a step back and analyze this query for a moment.
You're selecting everything from three out of four tables used in the query. The joins create some logic to limit what you select to the proper categories, authors, etc. At the end of the day you are getting a lot of data from the database, then in PHP simply asking it how many rows were returned (mysql_num_rows). Instead, what #Dagon is trying to suggest in comments, is that you have MySQL simply count the results, and return that.
Let's refactor your query:
$query = "
SELECT COUNT(t.post_id) AS qty
FROM posts as t
LEFT JOIN relations AS r ON r.post_id = t.post_id
LEFT JOIN users AS u ON t.auther_id = u.auther_id
LEFT JOIN categories AS c ON c.cate_id = r.cate_id
GROUP BY t.post_id
";
$result = mysql_query($query);
$result_row = mysql_fetch_assoc($result);
$numberOfPosts = $result_row['qty'];
(You could also use Barattlo's custom execute_scalar function to make it more readable.)
I would need to see your table structures to be of more help on how to optimize the query and get the desired results.
try doing this:
$Query="
SELECT count(t.*) as count_all
FROM posts as t
LEFT JOIN relations as r on r.post_id = t.post_id
LEFT JOIN users as u on t.auther_id = u.auther_id
LEFT JOIN categories as c on c.cate_id = r.cate_id
GROUP BY t.post_id
";
$Query=mysql_query($Query);
$numberOfPosts=mysql_num_rows($Query);
You want to do
SELECT count(t.id) AS count FROM ....
//do query with PDO or whatever you are using
$rows = mysql_fetch_assoc();
$num_rows = $rows['count'];
You should probably simply use
SELECT count(*) as postingcount FROM posts
Why?
Because you do not have a WHERE clause, so there are no restrictions. Your JOINS do not ADD more rows to the resultset, and in the end your GROUP BY merges every duplicate occurance of a post_id that might have occurred because of joining back into one row. The result should only be counted, so assuming that the real number you want to know is the number of data sets inside the table posts, you do not need any join, and doing count(*) really is a very fast operation on tables in MySQL.
Remember to check if mysql_query returns false, because then you have to check mysql_error() and see why your query has an error.

Mysql GROUP BY or DISTINCT query usage in PHP

I'm a little bit confused about these kind of new query types for me (DISTINCT,GROUP BY etc.)
I have three tables in my database.
BRANDS
PRODUCTS
RECIPES
What I would like to do is to list BRANDS then look for PRODUCTS of that brand and look if there is a RECIPE of those products, then I want it to be written.
I'm able to write it with the basic queries;
$sql = "SELECT * FROM BRANDS";
$sql2 = "SELECT * FROM PRODUCTS WHERE BRANDID = 1";
$sql3 = "SELECT * FROM RECIPES WHERE PRODUCTID = 2";
but it repeats the brands.As far as I googled I have to use GROUP BY or DISTINCT in my query.
How should I create my query layout and which of them I should use?
Thank you.
I don't think you necessarily need to do all the distinct and grouping, unless your database has duplicate rows. You can select all of that in one query like so:
Select Brands.BrandId, Brands.BrandName, Products.ProductId, Products.ProductName, Recipes.*
From Brands
Inner Join Products On Products.BrandId = Brands.BrandId
Inner Join Recipes On Recipes.ProductId = Products.ProductId
Where
Brands.BrandId = 1 And
Products.ProductId = 2
Order By Brands.BrandId, Products.ProductId, Recipes.RecipeId
Please note that I assumed you have a Brands.BrandName and Products.ProductName column. This query will get you a complete recordset, filtered based on Brand and Product.

PHP/MySQL Query In a loop, how to append to just one query?

I have a query to pull all articles out of the database..
$get_articles = $db->query("
SELECT *, a.id AS id, s.type AS type FROM ".$prefix."articles a
LEFT JOIN ".$prefix."sources s ON (s.id = source_id)
WHERE a.type!='trashed' ORDER BY a.timestamp DESC LIMIT $start, $end");
Within the loop of this query, I do then do another query on the same table to find related articles to the 'title' of the article, stored as '$related_phrase'. The query within the loop is:
// get related articles to this entry
$get_related = $db->query("
SELECT *, a.id AS id, MATCH (title, description) AGAINST ('$related_phrase') AS score FROM ".$prefix."articles a
LEFT JOIN ".$prefix."sources s ON (s.id = source_id) WHERE a.type!='trashed' AND MATCH (title, description) AGAINST ('$related_phrase') AND a.id!='$articles[id]' HAVING score > 7
ORDER BY a.timestamp DESC LIMIT 0, 3");
This basically means we have a query in a loop which is causing the pages to load very slowly.
What we want to do, is bring the query from within the loop, in the main query, so it's all working within one query, if that's possible?
Any help very much appreciated!
I don't think you would gain much speed by merging the two queries.
One thing you could try is to get a list of all articles and DISTINCT searchphrases (in e.g. temptable), and then build a query to get all related articles in one single go. Lastly match up related articles with the article list.
try this:
$articles_and_related = $db->query("
SELECT *
FROM ".$prefix."articles art
LEFT JOIN (
SELECT * FROM ".$prefix."articles x
WHERE
score > 7
AND x.type != 'trashed'
AND x.id != art.id
AND MATCH(x.title, x.description) AGAINST (art.title)
LIMIT 3
) rel
LEFT JOIN ".$prefix."sources s2 ON (s2.id = rel.source_id)
LEFT JOIN ".$prefix."sources s ON (s.id = art.source_id)
WHERE
art.type!='trashed'
ORDER BY art.timestamp DESC LIMIT $start, $end");

Categories