I am facing a really wierd issue, that i cannot GROUP by. If i do, it won't show the "best time". It's kinda wierd.
Here is the SQL String which works (But it shows the same quiz 4 times, which it shouldn't!)
SELECT distinct a.quiz_id, a.time, b.end, b.images, b.prize_title
FROM entries a, quiz b
WHERE a.quiz_id = b.id AND a.member = '".$_SESSION['id']."'
ORDER BY a.time ASC
Screen of output: http://imgur.com/mOfBK3q
This show NOT the best time (Fastest time => ASC)
SELECT distinct a.quiz_id, a.time, b.end, b.images, b.prize_title
FROM entries a, quiz b
WHERE a.quiz_id = b.id AND a.member = '".$_SESSION['id']."'
GROUP BY a.quiz_id
ORDER BY a.time ASC
Screen of output: http://imgur.com/QiNQZY4
The column "a.time" is miliseconds and the column is DOUBLE.
Try using HAVING instead of WHERE.
Avoid DISTINCT and GROUP BY in the same query (you only need one or the other).
Try something like this.
SELECT MIN(a.time), a.quiz_id, a.time, b.end, b.images,
b.prize_title FROM entries a, quiz b WHERE a.quiz_id = b.id AND
a.member = '".$_SESSION['id']."' GROUP BY a.quiz_id
The query doesn't work as expected because GROUP BY is applied before ORDER BY. It would just sort the results after the grouping -- and there's only one result left.
Try the following:
SELECT a.quiz_id, a.time, b.end, b.images, b.prize_title
FROM entries a
JOIN quiz b ON a.quiz_id = b.id
ORDER BY a.time ASC
LIMIT 1
Related
I have 2 tables, borrowers and loans. I want to display on the main page the list of ALL borrowers with or without loans. If with loan, display the newest one.
I have the following sql query, basically it returns the above description except it displays the very first loan of the borrower instead of the latest one.
(Side note: I used GROUP BY to avoid duplicates. Without it the query returns duplicated borrower names if they have multiple loans. Just wanted to know if this is an efficient way of doing so.)
SELECT b.b_id,
b.isdeleted,
b.picture,
b.firstname,
b.middlename,
b.lastname,
b.address,
b.contactno,
b.birthday,
b.businessname,
b.occupation,
b.comaker,
b.comakerno,
b.remarks,
b.datecreated,
b.activeloan,
l.l_id,
l.amount,
l.payable,
l.balance,
l.mode,
l.term,
l.interestrate,
l.amortization,
l.releasedate,
l.duedate,
l.status,
l.c_id
FROM borrowers as b
LEFT JOIN loans as l ON b.b_id = l.b_id
WHERE b.isdeleted = 0
GROUP BY b.b_id
It seems the below query does exactly what i wanted.
I added the below subquery on the "ON" clause.
(SELECT MAX(l_id)
FROM jai_db.loans as l2
WHERE l2.b_id = b.b_id LIMIT 1)
SELECT b.b_id, b.isdeleted, b.picture, b.firstname, b.middlename, b.lastname, b.address, b.contactno,
b.birthday, b.businessname, b.occupation, b.comaker, b.comakerno, b.remarks, b.datecreated, b.activeloan,
l.l_id, l.amount, l.payable, l.balance, l.mode, l.term, l.interestrate, l.amortization,
l.releasedate, l.duedate, l.status, l.c_id
FROM jai_db.borrowers as b
LEFT JOIN jai_db.loans as l
ON l.l_id = (SELECT MAX(l_id)
FROM jai_db.loans as l2
WHERE l2.b_id = b.b_id LIMIT 1)
WHERE b.isdeleted = 0
I'm joining two tables to display car brands. Here is the structure:
SELECT DISTINCT
b.title
FROM
brands as b
INNER JOIN items as i
ON i.brand_id = b.id
WHERE i.status = 1
ORDER BY COUNT(i.brand_id) DESC;
The above only produces one record. If I remove "ORDER BY COUNT(i.brand_id) DESC;" it displays all the records correctly.
I would like to sort result based on number of vehicles under each brand category. So for example if bmw category has the most car listed under, it should be the first one.
SELECT b.title
FROM brands as b
INNER JOIN items as i
ON i.brand_id = b.id
WHERE i.status = 1
GROUP BY b.title
ORDER BY COUNT(i.brand_id) DESC;
This should work for you.
I would use
SELECT b.title, count(i.brand_id)
FROM items i
LEFT JOIN brands as b
ON b.id = i.brand_id
WHERE i.status = 1
GROUP BY i.brand_id
ORDER BY COUNT(i.brand_id) DESC;
Your concern is the amount of cars in inventory. You want to break them down by how many of each brand you have. So, you're mainly concerned with the items tables and only need the brands table to get the information stored in the items table (brand name). Lastly, in order to get aggregate the number of brands of each, you must let MySQL know what you want in the GROUP BY.
I haven't used an EXPLAIN, but I would think the bottom query is more efficient.
You could join brands on an aggregate query:
SELECT b.title
FROM brands b
INNER JOIN (SELECT brand_id, COUNT(*) AS cnt
FROM items
WHERE status = 1
GROUP BY brand_id) i ON i.brand_id = b.id
ORDER BY cnt DESC;
I have a problem with one SQL query.
So I have two tables:
Animelist + Animeepisode and I would like to get last 8 rows from animelist joined with last animeepisodes. So far I have something like this:
SELECT MAX(a.id) AS id, a.id_anime AS id_anime, a.nazev AS nazev, b.nazev AS nazev_anime, b.id AS idd
FROM animedily AS a
INNER JOIN animelist AS b ON a.id_anime = b.id
GROUP BY b.id
ORDER BY a.id DESC LIMIT 0,8
Which give me LAST anime from animelist (exactly what I need :-)), but every first episode from animeepisodes(I need last :-p)
Thanks for any help :-)
I have a query below. On executing it, it returns all the results where i can see multiple records for each single memberid.
I want to select the profiles of two members randomly chosen without any performance issue.
SELECT
a.memberid,
a.category_id,
a.content,
a.count_cid,
a.importance
FROM tb_profilingdata a,
tb_member b
WHERE a.memberid = b.memberid
AND a.category_id IN($catstr)
AND a.memberid NOT IN('$mid',$seen)
AND b.gender = 'male'
ORDER BY a.memberid, a.category_id
I tried some queries for choosing one random record
SELECT
r1.memberid
FROM tb_profilingdata AS r1
JOIN (SELECT
(RAND() * (SELECT MAX(DISTINCT(memberid)) FROM tb_profilingdata)) AS memberid) AS r2
WHERE r1.memberid >= r2.memberid
ORDER BY r1.memberid ASC
LIMIT 1
But it chooses in total 1 record out of tb_profilingdata whereas i want records of one randomly chosen member.
I tried the same query with tb_member, but it is possible that a member present in tb_member might not have its entries in tb_profilingdata..
Please suggest me a good way out with least performance issues.
May be that will help you:
SELECT [something] FROM [source] WHERE [conditions] ORDER BY RAND() LIMIT 2
This query is untested unless you provide some sample data
SELECT
a.memberid,
a.category_id,
a.content,
a.count_cid,
a.importance
FROM tb_profilingdata a
left join (select
memberid
from tb_member
group by memberid
order by rand()) b
on a.memberid = b.memberid
WHERE a.category_id IN($catstr)
AND a.memberid NOT IN('$mid',$seen)
AND b.gender = 'male'
ORDER BY a.memberid, a.category_id
This is my first attempt at a JOIN MySQL statement...
I have 2 tables..games and games_ratings
both tables have an id column. the id represents the id of the game. and i only want to get the average of the ints in the rating column where the id in games_ratings is equal to the id from the games table.
SELECT a.id, a.name, AVG(b.rating) AS average FROM games a LEFT JOIN games_ratings b GROUP BY a.id ORDER BY average DESC LIMIT 50;
any ideas?
Try this:
SELECT a.id, a.name, AVG(b.rating) AS average
FROM games a
LEFT JOIN games_ratings b
ON a.id = b.id # <-- You need this line I believe
GROUP BY a.id
ORDER BY average DESC LIMIT 50;
Edit: This is a bit hard without your complete schema, but you can try something like this.
SELECT a.id, a.name, AVG(b.rating) AS average, COUNT( b.id) as votes
FROM games a
LEFT JOIN games_ratings b
ON a.id = b.id
GROUP BY a.id
ORDER BY votes DESC, average DESC LIMIT 50; # <-- You may need to modify this line
Don't forget the WHERE clause he asked for:
where the id in games_ratings is equal to the id from the games table
> SELECT a.id, a.name, AVG(b.rating) AS average
> FROM games a
> LEFT JOIN games_ratings b
> ON a.id = b.id # <-- You need this line I believe
> **WHERE a.id = b.id**
> GROUP BY a.id
> ORDER BY average DESC LIMIT 50;