Below is the mysql query:
SELECT * FROM `is_product_info` ORDER BY (select distinct product_id from is_product_images where is_product_images.product_id = is_product_info.id) asc
I am not getting how the order by works with subquery here.
I came across such queries having order by with subquery in the wordpress plugin. This is new to me so I am asking if someone else knows this. Below is such example:
SELECT wp22_posts.*
FROM wp22_posts
LEFT
JOIN wp22_term_relationships
ON wp22_posts.ID = wp22_term_relationships.object_id
WHERE 1=1
AND wp22_posts.ID IN(260,412,413,414,415,469,474,483
,485,487,488,515,516,563,568,569
,584,592,593,596,599,601,604,610
,612,672,675,678,681,690,860,861,862,863)
GROUP
BY wp22_posts.ID
ORDER
BY (SELECT distinct meta_value from wp22_postmeta where'meta_key' LIKE 'price') ASC
LIMIT 0, 10
Is there anybody who describe this query?
To answer your question, the ORDER BY clause does nothing at all (other than potentially stop the query from working).
Related
I want to select which model car is involved in most accidents.
i am using the following query but i get a syntax error.
please someone tell me whats wrong..
select car.Model
from car
join car_accident_involved
on car.Car_Registration_ID = car_accident_involved.Car_Registration_ID
group by car.Model
having MAX(
select COUNT(Car_Registration_ID)
from car_accident_involved
);
You can use a simple sub query here, e.g:
select model from car
where car_registration_id =
(select car_registration_id
from car_accident_involved
group by model
order by count(car_registration_id) desc
limit 1);
HAVING is a condition statement for GROUP BY. Your query hasn't any condition in
HAVING statement,so error arises.
A for me, there is no need in subquery. Try more simple query like:
SELECT c.model,COUNT(a.car_registration_id) AS Num_of_accidents FROM car c
INNER JOIN car_accident_involved a ON c.car_registration_id=a.car_registration_id
GROUP BY c.model ORDER BY Num_of_accidents DESC LIMIT 1;
I am developing a voting system.
I am facing issue in ordering.
Basically i want to get top ranking and maximum nominee first. I used "totalUserVoted" and "totalRating" in Descending order on both condition but my query ordering "totalUserVoted".
I am expecting the result in this order.
Here is my sql query.
SELECT
(SELECT (((SUM(`design`)*4)+(SUM(`usability`)*3)+(SUM(`creativity`)*2)+(SUM(`content`))*1))/ count(`nominee_id`) / 10
FROM `sk_award_nominee_rating`
WHERE `sk_award_nominee_rating`.`nominee_id`=`sk_award_nominee`.`nominee_id`) AS totalRating,
(SELECT count(`nominee_id`)
FROM `sk_award_nominee_rating`
WHERE `sk_award_nominee_rating`.`nominee_id`=`sk_award_nominee`.`nominee_id`) AS totalUserVoted,
`sk_award_nominee`.*,
`sk_user`.`username`,
`sk_user`.`email`,
`sk_user_profile`.`f_name`,
`sk_user_profile`.`m_name`,
`sk_user_profile`.`l_name`,
`sk_user_profile`.`address`
FROM `sk_award_nominee`
LEFT JOIN `sk_user` ON `sk_user`.`user_id`=`sk_award_nominee`.`user_id`
LEFT JOIN `sk_user_profile` ON `sk_award_nominee`.`user_id`=`sk_user_profile`.`user_id`
WHERE `sk_award_nominee`.`status` = 1
AND DATE(approval_date) = '2016-02-22'
ORDER BY `totalUserVoted` DESC,
`totalRating` DESC
Something like this ?
ORDER BY totalUserVoted DESC, totalRating DESC
PHP MySQL Order by Two Columns
Initially I need to build a query fetching sites from one table ordered by date of newest article (articles placed in the separate table).
I build the following query:
SELECT *
FROM `sites`
INNER JOIN `articles` ON `articles`.`site_id` = `sites`.`id`
ORDER BY `articles`.`date` DESC
GROUP BY `sites`.`id`
I supposed that SELECT and INNER JOIN will fetch all posts and associate a site to each one, than ORDER BY will order the result by descending of post date than GROUP BY will take the very first post for each site and I will get the needed result.
But I'm receiving MySQL error #1064 - 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 'GROUP BYsites.idLIMIT 0, 30' at line 7
If I place GROUP BY before ORDER BY statement the query is working but it will not give me the newest post for each site. Instead the result will be sorted after the grouping which is not the thing I need (actually I could prefer to order in another way after grouping).
I read several pretty similar questions but they all related to the data stored in a single table making it possible to use MAX and MIN functions.
What should I do to implement what I need?
You can use either a subquery / derived-table / inline-view or a self-exclusion join, e.g.:
SELECT s.*, a1.*
FROM `sites` s
INNER JOIN `articles` a1 ON a1.`site_id` = s.`id`
LEFT OUTER JOIN `articles` a2 ON a2.`site_id` = a1.`site_id`
AND a2.`date` > a1.`date`
WHERE
a2.`site_id` IS NULL
ORDER BY
a1.`date` DESC
The principle is that you select the sites for which there is no article date greater than any other article date.
rewrite the sql to the following syntax -
SELECT `articles`.`article_name`,'sites'.'id','articles'.'site_id'
FROM `sites`,'articles'
WHERE `articles`.`site_id` = `sites`.`id`
ORDER BY 'sites'.'id', `articles`.`date` DESC;
Do something like this in the select statement. Group by function demands that all fields to be grouped. Hence usage of * is not possible.
SELECT * FROM ( SELECT `S.<col1>`, `S.<col2>`, `A.<col1>`,`A.<col2>`,
ROW_NUMBER ()
OVER (PARTITION BY `SITES`.`ID`
ORDER BY `SITES`.`ID` DESC)
RID
FROM `SITES` `S`,`ARTICLES` `A`
WHERE `ARTICLES`.`SITE_ID` = `SITES`.`ID`
)
WHERE RID = 1;
Can you try this?
Finally I came to the solution.
First of all I changed the main query from queering from sites table to queering from articles. Next I added the MAX(date) column to the result.
So the resulting query implementing the thing I need is the following:
SELECT `sites`.`url`,MAX(`articles`.`date`) AS `last_article_date`
FROM `articles`
INNER JOIN `sites` ON `sites`.`id` = `article`.`site_id`
GROUP BY `site_id`
ORDER BY `last_article_date` ASC
Thanks to all of you for giving me hints and right search directions!
I have the following query, which was developed from a hint found online because of a problem with a GROUP BY returning the maximum value; but it's running really slowly.
Having looked online I'm seeing that WHERE IN (SELECT.... GROUP BY) is probably the issue, but, to be honest, I'm struggling to find a way around this:
SELECT *
FROM tbl_berths a
JOIN tbl_active_trains b on a.train_uid=b.train_uid
WHERE (a.train_id, a.TimeStamp) in (
SELECT a.train_id, max(a.TimeStamp)
FROM a
GROUP BY a.train_id
)
I'm thinking I possibly need a derived table, but my experience in this area is zero and it's just not working out!
you can move that to a SUBQUERY and also select only required columns instead of All (*)
SELECT a.train_uid
FROM tbl_berths a
JOIN tbl_active_trains b on a.train_uid=b.train_uid
JOIN (SELECT a.train_id, max(a.TimeStamp) as TimeStamp
FROM a
GROUP BY a.train_id )T
on a.train_id = T.train_id
and a.TimeStamp = T.TimeStamp
I'm trying to count freelanceFeedback's and order by the count like this:
$sql = "SELECT authentication.*, (SELECT COUNT(*) FROM freelanceFeedback) as taskscount FROM authentication
LEFT JOIN freelanceFeedback
ON authentication.userId=freelanceFeedback.FK_freelanceWinnerUserId
WHERE `FK_freelanceProvider`=$what
ORDER BY taskscount DESC";
But I'm having multiple outputs if the user has multiple feedbacks and it's not ordering by the taskscount.
I can't figure out what the 'tweet' is wrong..
** UPDATE **
I think I've got it myself:
$sql = "SELECT DISTINCT authentication.*,
(SELECT COUNT(*) FROM freelanceFeedback
WHERE FK_freelanceWinnerUserId=userId
) as taskscount
FROM authentication
WHERE `FK_freelanceProvider`=$what
ORDER BY taskscount DESC";
This is only outputting 1 user and ORDERING by the amount of feedbacks.
When you use COUNT(), you also need to use GROUP BY:
SELECT authentication.userId,
COUNT(freelanceFeedback.id) AS taskscount
FROM authentication
LEFT JOIN freelanceFeedback
ON authentication.userId = freelanceFeedback.FK_freelanceWinnerUserId
WHERE `FK_freelanceProvider`= $what
GROUP BY authentication.userId
ORDER BY taskscount DESC
However, this will only work if you are not doing SELECT * (which is bad practice anyway). Everything that's not in the COUNT bit needs to go into GROUP BY. If this includes text fields, you'll not be able to do it, so you'll need to do a JOIN to a subquery. MySQL won't complain if you don't but it can seriously slow things down and other DBs will throw an error, so best to do it right:
SELECT authentication.userId,
authentication.textfield,
authentication.othertextfield,
subquery.taskscount
FROM authentication
LEFT JOIN (SELECT freelanceFeedback.FK_freelanceWinnerUserId,
COUNT(freelanceFeedback.FK_freelanceWinnerUserId) AS taskscount
FROM freelanceFeedback
GROUP BY FK_freelanceWinnerUserId) AS subquery
ON authentication.userId = subquery.FK_freelanceWinnerUserId
WHERE authentication.FK_freelanceProvider = $what
ORDER BY subquery.taskscount DESC
It's not clear what table the FK_freelanceProvider is part of so I've assumed it's authentication.