I added below query in phpmyadmin
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
Query Executed.
I got below output showing (0- 30 results) total 24000 records found
I tried same query in different format
SELECT count(u_id) As cnt,id,u_id
FROM `pager`
WHERE type = 'profile' group by u_id
Query Executed.
I got below Query Executed Successfully.SHowing just 30 records , No Pagination dropdown.I can't click id and u_id field heading to sort..Someone please help what is the issue..I think both query should execute 24000 records..Please advise
PhpMyAdmin display default results as per it's settings which you can change from Number of rows : dropdown value in the interface.
If you need to display all your result, then you must add limit clause in your query to display all records
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
LIMIT 0, 999999
Otherwise, it will display 30 resuts (or whatever default PhpMyAdmin settings)
NOTE: To sort out the results, add Order by clause in your query then the query would look like following:
SELECT id,u_id,count(u_id) As cnt
FROM `pager`
WHERE type = 'profile' group by u_id
ORDER BY `id` DESC
LIMIT 0, 999999
Hope, it helps you.
Related
I have a "reply" table with structure.
replyno topicno replydesc replyrank
Now i need to retrieve top 2 records ordered by replyrank in descending order (means first 2 high ranked records) for each topicno (which is a foreign key).
I need a query in mysql that can extract result set like this for all topic numbers.
please give me optimized query that can execute faster
Try this:
SELECT replyno, topicno, replydesc, replyrank
FROM (SELECT replyno, topicno, replydesc, replyrank,
IF(#topicno = #topicno:=topicno, #id:=#id+1, #id:=1) AS id
FROM reply, (SELECT #id:=1, #topicno:=0) A
ORDER BY topicno, replyrank DESC
) AS A
WHERE id <= 2;
Try this Query i think it gonna work for you
SELECT replyno, topicno FROM reply ORDER BY replyrank DESC LIMIT 2
I have the following statement:
SELECT user_id, myMysqlFunction(user_id) FROM users_to_days GROUP BY user_id;
The table stores one row per user and day.
The problem is that MySql calls the Function "myMysqlFunction(user_id)" for every row and groups the rows after that.
My target is to execute the Function just once (for performance reasons).
I tried the following state without success:
SELECT user_id, IF(`date` = min(`date`),myMysqlFunction(user_id),0) FROM users_to_days GROUP BY user_id;
MySql still executes the statement for every row.
If I write a static date (for example '2014-09-01') - it works:
SELECT user_id, IF(`date` = '2014-09-01',myMysqlFunction(user_id),0) FROM users_to_days GROUP BY user_id;
But I don't know the min. date for the user at the time of execution. (its not the same for every user)
you could try a subquery:
SELECT user_id, myMysqlFunction(user_id)
FROM (SELECT user_id
FROM users_to_days
GROUP BY user_id);
Edit: comment got split up, I wanted to say:
I meant: try $date = date('Y-m-d');, that will get the time the user runs it. Put that in min($date), and it should be working.
I got specific query:
SELECT *
FROM stats
WHERE mod_name = 'module'
GROUP BY domain
ORDER BY addition_date DESC
I want to retrive, the newest value for every domain. I know there is a domain x.com value with date 2014-02-19.
However, this query returns me row with date: 2014-01-06
That's quite simple query... why it does not take group by domains and take only newest value?
Am I missing something?
The order by takes place after the group by. That is why your query does not work. Here is a way to get what you want:
SELECT s.*
FROM stats s
WHERE mod_name = 'module' and
not exists (select 1
from stats s2
where s2.mod_name = s.mod_name and
s2.addition_date > s.addition_date
)
ORDER BY addition_date DESC;
To get the best performance, create an index on stats(mod_name, addition_date).
I'm using this query to select a set of records from a MySQL database:
SELECT *
FROM table
WHERE id IN(10,14,12,11,8,7,4)
AND actief='on'
ORDER BY FIELD(id,10,14,12,11,8,7,4)
This will give me the given ID's. In this case I will get a maximum of 7 records. But it can be less if for example ID '14' has active='off'.
But what I need is a set of 20 records where het list IN(10,14,12,11,8,7,4) must be in the result if they also meet the condition active='on'. If this returns 6 records, then I want the query to select another 14 records. Selecting highest ID first, must meet active='on' and may not already be in the result.
Can this be achieved by one SQL statement. Or should I first put the result of the mentionend query in an array and in a second array select the remaining records. And finaly put those also in the array?
You want to sort rather than filter the results. I think this is the query you want:
SELECT *
FROM table
ORDER BY (id IN(10,14,12,11,8,7,4) AND actief = 'on') desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;
EDIT:
The final solution only wanted actief = 'on', so:
SELECT *
FROM table
WHERE actief = 'on'
ORDER BY (id IN (10,14,12,11,8,7,4)) desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;
I have a table with fields:
id, albumid, userid, keywords where keywords is varchar and can be more than one by delimteter eg : one,two,three
I want to get the top 10 results of the most popular keyword but not by the same user
I currently use this but not sure if its correct:
$tableName = $db->nameQuote('#__mytable');
$sql = "SELECT `id`,`albumid`,`userid`,`keywords`, COUNT(keywords) AS popular FROM ".$tableName." GROUP BY `userid` HAVING COUNT(*) > 1 ORDER BY popular DESC LIMIT ".$lim0.",".$lim;
$db->setQuery($sql);
Is the following code correct. Im not sure if im getting the keyword with most duplicate entries...
Try this query on for size, I just fudged around your groupings, and using DISTINCT userid as the counting metric
"SELECT `id`,`albumid`,`keywords`, COUNT(DISTINCT userid) AS popularity FROM ".$tableName." GROUP BY `keywords` HAVING popularity >= 1 ORDER BY popularity DESC LIMIT ".$lim0.",".$lim;
However, if your keywords column is comma delimited, this query will NOT work. This query operates on the fact that each record has a single keyword entry