How to find the distinct values with total number of rows? - php

I want result in a single query.
Currently, I am using 2 queries. First for the distinct rows and the second is for the count rows. Second query fire for each row of the first query. Which is not good for the server.
I tried it in a single query but it shows count as 1 for each row which is wrong. I don't know how to do it in a single query.
Please help!
2 queries:
SELECT DISTINCT `_car_make`.`id` as `make_id`, `_car_make`.`title`
FROM `_motor`, `_car_make`
WHERE `_motor`.`make` = `_car_make`.`id`
AND `domain` = 'domain.com' AND `active` = 'y' AND `_motor`.`status` != 'SOLD'
GROUP BY `_motor`.`id` ORDER BY `_car_make`.`title`
SELECT COUNT(DISTINCT `id`) AS `TOTAL_MAKE` FROM `_motor`
WHERE `_motor`.`make` = '$make_id'
AND `domain` = 'domain.com' AND `_motor`.`status` != 'SOLD' AND `active` = 'y'
I tried it in 1 query, which shows count as 1 which is wrong:
SELECT DISTINCT `_car_make`.`id` as `make_id`, `_car_make`.`title`, count(`_motor`.`id`) AS TotalMake
FROM `_motor`, `_car_make`
WHERE `_motor`.`make` = `_car_make`.`id`
AND `domain` = 'domain.com' AND `active` = 'y' AND `_motor`.`status` != 'SOLD'
GROUP BY `_motor`.`id` ORDER BY `_car_make`.`title`

You can try below-
SELECT `_car_make`.`id` as `make_id`, `_car_make`.`title`, count(*) AS TotalMake
FROM `_motor` inner join `_car_make`
on `_motor`.`make` = `_car_make`.`id`
AND `domain` = 'coxheathcarcentre.co.uk'
where `active` = 'y' AND `_motor`.`status` != 'SOLD'
GROUP BY `_car_make`.`id`,`_car_make`.`title`
ORDER BY `_car_make`.`title`

Related

Multiple exists queries not returning anything (SQL)

When I run single exist query I get results as expected:
SELECT *
FROM `slasher_farming_mods`
WHERE EXISTS (SELECT * FROM `slasher_farming_brands`
WHERE `slasher_farming_mods`.`brand_id` = `slasher_farming_brands`.`id`
AND `brand_id` = '7'
ORDER BY `id` asc)
LIMIT 4 OFFSET 0
When I run multiple exist queries, I don't get any results:
SELECT *
FROM `slasher_farming_mods`
WHERE EXISTS (SELECT * FROM `slasher_farming_brands`
WHERE `slasher_farming_mods`.`brand_id` = `slasher_farming_brands`.`id`
AND `brand_id` = '7'
ORDER BY `id` ASC)
AND EXISTS (SELECT * FROM `slasher_farming_brands`
WHERE `slasher_farming_mods`.`brand_id` = `slasher_farming_brands`.`id`
AND `brand_id` = '24'
ORDER BY `id` ASC)
LIMIT 4 OFFSET 0
Tried using debugbar in laravel to see if my query is taking too long, but it takes less than 1ms. What could be wrong here, I tried to run these query also directly inside phpmyadmin but still no results with more than one where exists.
Query is populated by foreach loop in laravel.
foreach ($brands as $brand){
$query->whereHas('brand', function($q) use ($brand){
$q->where('brand_id', '=', $brand)->orderBy('id');
});
}
}
Your query doesn't work because brand_id cannot be both 7 and 24 for a given record. You want "OR". It would be clearer to just use the expression brand_id in (7, 24) instead of the two separate sub-queries. There is no point in sorting the sub-query.
Alternatively, join the two tables:
select ...
from slasher_farming_mods m
join slasher_farming_brands b on m.brand_id = b.id
where brand_id in (7, 24)
LIMIT 4 OFFSET 0

SQL search Result, Group by ID number

I'm having some issues with trying to fix this SQL Query
This is a custom search query which is searching for the word 'weddings' on all pages on this CMS system.
At the moment I am getting the same page appear on the first 5 rows because the word 'weddings' appears 5 times. What I want to do is combine the rows with the same ID number into 1 row so it doesn't appear multiple times.
I thought doing a group by at the end of this statement would do this but I keep getting an SQL syntax error
GROUP BY `documents`.`id`
I have attached the full SQL bellow with an image of the output i currently get.... Any idea?
SELECT `documents`.*,
`documenttypes`.`name` as `doctype`,
`articles`.`id` as `article_id`,
`articles`.`language_id`,
`articles`.`title`,
`articles`.`template`,
`articles`.`slug`,
`articles`.`path`,
`articles`.`slug_title`,
MATCH ( elements.textvalue )AGAINST ( 'weddings' ) AS score,
elements.textvalue AS matching,
LOWER(`articles`.`title`)
LIKE '%weddings%' as 'like_title',
( MATCH ( elements.textvalue )
AGAINST ( 'weddings' ) ) + IF(( LOWER(`articles`.`title`)
LIKE '%weddings%'),1, 0) + IF((LOWER(`elements`.`textvalue`)
LIKE '%weddings%'),1, 0) as total FROM (`documents`)
LEFT JOIN `articles` ON `articles`.`document_id` = `documents`.`id`
LEFT JOIN `documenttypes` ON `documents`.`documenttype_id` = `documenttypes`.`id`
LEFT JOIN `documents_users` AS du ON `documents`.`id` = du.`document_id`
LEFT JOIN `documents_usergroups` AS dug ON `documents`.`id` = dug.`document_id`
LEFT JOIN elements ON `elements`.`article_id` = `articles`.`id`
WHERE `documents`.`trashed` = 0
AND `documents`.`published` = 1
AND `articles`.`status_id` = 1
AND `articles`.`language_id` = 1
AND (`documents`.`no_search` = '0'
OR `documents`.`no_search` IS NULL)
AND ( (dug.usergroup_id IS NULL)
AND (du.user_id IS NULL) )
AND (`documents`.`startdate` < NOW()
OR `documents`.`startdate` = '0000-00-00 00:00:00' OR `documents`.`startdate` IS NULL)
AND (`documents`.`enddate` > NOW()
OR `documents`.`enddate` = '0000-00-00 00:00:00'
OR `documents`.`enddate` IS NULL)
HAVING (total > 0)
ORDER BY label ASC,
total DESC LIMIT 0,10
You can try to use the statement DISTINCT:
SELECT DISTINCT 'documents'.*,
'documenttypes'.'name' as 'doctype',
'articles'.'id' as 'article_id',
...
GROUP BY lets you use aggregate functions, like AVG, MAX, MIN, SUM, and COUNT which apparently you don't use.

mysql get max value with multiple choices

i have this poll thing, Poll A, has 3 options let's say: Option a option b option c, option a got 3 votes, option b got 2 votes option c got 3 votes,
OPTIONS VOTES
option a 3
option b 2
option c 3
and i have this mysql query which gets the options and orders by votesCount, limit 1 to get the top answer, but in my example, there are two options on top, they both have the highest values, i want a query to get those two options, n not only one, so i have to get rid of LIMIT 1
Mysql query is
$query = "SELECT `option` FROM `options` WHERE `pid` = '$pid' AND `votesCount` != '0' ORDER BY `votesCount` DESC LIMIT 1";
any suggestion?
Here is a standard way in any SQL dialect:
select p.*
from poll p
where p.votes = (select max(votes) from poll)
Thanks to #Gordon Linoff for the hint, this is how it is now
$query = "SELECT `option` FROM `options` WHERE `pid` = '$pid' AND `votesCount` = (SELECT MAX(`votesCount`) FROM `options` WHERE `pid` = '$pid' ORDER BY `votesCount` DESC LIMIT 1)";
///Just a DB function, don't mind the 0, i'm using a class
$res = $db->get_rows($db->select($query),0);
$merged = array();
foreach ($res as $r){
$merged[] = $r->option;
}
$merged = implode(',',$merged);
return $merged;

Sorting mysql results by times from 3 tables

I have 3 tables: questions, articles and pictures
The rows in each table contain a current_timestamp column posted, and link to an id. I'd like to sort the results of the rows of all three by their timestamp and only echo the newest of the three (for example: if the question is newest from the ID, display that only)
if(count($interests) != 0){ foreach($interests as $interests_following){
$interestid = mysql_result(mysql_query("SELECT `id` FROM `interests` WHERE `name` = '$interests_following'"),0);
$interestquestions = #mysql_result(mysql_query("SELECT `question_text`, `posted` FROM `questions` WHERE `interest` = '$interests_following'"),0);
$interestarticles = #mysql_result(mysql_query("SELECT `article_title`, `posted` FROM `articles` WHERE `interest_id` = '$interestid'"),0);
$interestpictures = #mysql_result(mysql_query("SELECT `interest_pic_title`, `posted` FROM `interest_pictures` WHERE `interest_id` = '$interestid'"),0);
echo '.$interests_following.': //<Only display 1 newest item (article/picture/question here>
Use UNION ALL:
SELECT posted
FROM
(
SELECT `question_text`, `posted` FROM `questions`
WHERE `interest` = '$interests_following'
UNION ALL
SELECT `article_title`, `posted` FROM `articles`
WHERE `interest_id` = '$interestid'
UNION ALL
SELECT `interest_pic_title`, `posted` FROM `interest_pictures`
WHERE `interest_id` = '$interestid'
) t
ORDER BY posted DESC LIMIT 1
To get a single row back:
$sql = "SELECT * FROM WHERE `name`='$interests_following' ORDER BY `posted` DESC LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
All of the work is in the SQL
ORDER BY `posted` DESC LIMIT 1
This orders results to have the newest first, then only return the first row.
If you mean to return only one item out of the 3 tables, you have 2 choices:
Fetch a row from each and then determine the newest in PHP
Use a UNION to find the single newest row in SQL
The latter makes for less code, but will probably be less readable.
try this
$interestquestions = #mysql_result(mysql_query("SELECT `question_text`, `posted` FROM `questions` WHERE `interest` = '$interests_following' ORDER BY timestamp_field DESC limit 1"),0);
this will show only the latest data base on your timestamp.
I'll show you the SQL queries so you can see what I did; adapt it to your code as needed.
SELECT
(SELECT question_text
FROM questions ORDER BY posted DESC LIMIT 1 ),
(SELECT article_title
FROM articles ORDER BY posted DESC LIMIT 1 ),
(SELECT interest_pic_title
FROM interest_pictures ORDER BY posted DESC LIMIT 1)
;
This sorts by the timestamp DESCending, so the latest record is first, then limits it to the single record. The result will be question_text, article_title, interest_pic_title.

How do you ORDER BY two tables?

I am what you would call a 'noob' at MySQL. I can insert/edit/select stuff, but anything more advanced than that stumps me. I have two tables in my database:
Table 'reviews'
id int(11)
review varchar(2500)
game int(11)
user int(11)
title varchar(200)`
and Table 'review_rating'
user int(11)
review int(11) // Corresponds to `reviews.id`
like tinyint(1)
Here is my question: Is it possible to use ORDER BY on the reviews table to order the result by the total number of review_ratings with 'like' = 1 (where 'review' = the id of the 'reviews' table) divided by the total number of review_ratings (where 'review' = the id of the 'reviews' table).
Example:
SELECT *
FROM `reviews`
WHERE `game` = ?
ORDER BY (total number of review_ratings where review = reviews.id and like = 1 /
total number of review_ratings where review = reviews.id)
LIMIT 0, 10
SELECT t.review,
Score = CASE WHEN TotalReviews<> 0 THEN LikedReviews/TotalReviews ELSE NULL END
FROM (
SELECT *,
(SELECT COUNT(*) FROM review_rating WHERE review = r.review) AS TotalReviews ,
(SELECT COUNT(*) FROM review_rating WHERE review = r.review AND like = 1) AS LikedReviews,
FROM review r
WHERE game = ?
)t
ORDER BY t.review, Score
I think it's clearer to put it in the SELECT clause:
SELECT reviews.*,
( SELECT SUM(like) / COUNT(1)
FROM review_ratings
WHERE review = reviews.id
) like_ratio
FROM reviews
WHERE game = ?
ORDER
BY like_ratio DESC
LIMIT 10
;
Notes:
Not tested; I'm away from a MySQL box at the moment.
I think you could move the subquery to the ORDER BY clause if you wanted, but it seems like a useful thing to retrieve, anyway.
I'm not sure how the above will behave if a given review has no ratings. You may need to use a CASE expression to handle that situation.
something like this would order by the total review_rating per review:
select( count(review.id) as 'total' from reviews join review_rating on review.id = review_rating.review group by review.id) order by total
the math is not exactly what you had but hopefully you will get it

Categories