I am working on mysql querying a table which keeps on increasing every minute. The query is also being used for pagination also having the limit clause. The table consist of user session being visited on store website. I have already added indexes for store_id, session_id
Following is the query.
SELECT *,
GROUP_CONCAT(email) as email_id
FROM
(
SELECT *
FROM clickstream
WHERE clickstream.store_id = ''
ORDER BY id DESC
) as clck
GROUP BY session_id
ORDER BY server_now DESC
LIMIT ".$offset.",".PER_PAGE_PRODUCT_COUNT
Any ideas?
Your index on store_id will help you in this query, you can't Speed the query up further by other indices. You just can remove the subquery:
SELECT
*,
GROUP_CONCAT(email ORDER BY id) as email_id
FROM
clickstream
WHERE
clickstream.store_id = ''
GROUP BY
session_id
ORDER BY
server_now DESC
LIMIT
$offset,PER_PAGE_PRODUCT_COUNT
Related
I have a MySQL table with the columns "user" and "warningpoints" for each warning as you can see in the table above. How can I get the user which has the most warningpoints in total in PHP?
You can use GROUP BY and ORDER BY and LIMIT:
SELECT t.user,sum(t.warningPoints) as sum_points
FROM YourTable t
GROUP BY t.user
ORDER BY sum_points DESC
LIMIT 1;
Or if there is only one record per person, no need to group :
SELECT t.user,t.warningPoints
FROM YourTable t
ORDER BY t.warningPoints DESC
LIMIT 1;
You can simply add below statements in your php code.
$sql = "SELECT User, Max(warningpoints) AS MaxWarningpoints FROM MyGuests GROUP BY User";
$result = $conn->query($sql);
I would like to get number of all records and get last record :
$sql_count_sms = "SELECT count(*) as total,content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = $row_num_sms['total'];
$last_my_sms = $row_num_sms['content'];
I can get number of records but I can't get last content record .
It returns first record !
Where is my wrong ?
Below codes works fine, but I think count(*) is faster than mysql_num_rows .
$sql_count_sms = "SELECT content,id FROM android_users_sms WHERE user_id=$id ORDER BY id DESC";
$result_count_sms = mysql_query($sql_count_sms);
$row_num_sms = mysql_fetch_assoc($result_count_sms);
$num_sms = mysql_num_rows($result_count_sms);
$last_my_sms = $row_num_sms['content'];
Any solution?
The grain of the two results you want is not the same. Without using a sub-query you can't combine an aggregate and a single row into the same result.
Think of the grain as the base unit of the result. The use of GROUP BY and aggregate functions can influence that "grain"... one result row per row on table, or is it grouped by user_id etc... Think of an aggregate function as a form of grouping.
You could break it out into two separate statements:
SELECT count(*) as total FROM android_users_sms WHERE user_id = :id;
SELECT * FROM android_users_sms WHERE user_id = :id ORDER BY id DESC LIMIT 1;
Also, specific to your question, you probably want a LIMIT 1 in combination with the ORDER BY to get just the last row.
Now, counter intuitively perhaps, this should also work:
SELECT count(*), content, id
FROM android_users_sms
WHERE user_id = :id
GROUP BY id, content
ORDER BY id
LIMIT 1;`
This is because we've changed the "grain" with the GROUP BY. This is the real nuance and I feel like this could probably be explained better than I am doing now.
You could also do this with a sub query like so:
SELECT aus.*,
(SELECT count(*) as total FROM android_users_sms WHERE user_id = :id) AS s1
FROM android_users_sms AS aus
WHERE user_id = :id ORDER BY id DESC LIMIT 1;
Look at my code, I want the select statement order by the count percentage after I fetch the data from this select statement, obviously, it's not logical. What can I do? Help, appreciate.
<?php
//myslq connection code, remove it because it's not relate to this question
$stm =$db->prepare("SELECT id ,term_count, COUNT(user_id) as count FROM sign WHERE term IN (:term_0,:term_1) GROUP BY user_id ORDER by count DESC");
//trying replace order by count with $combine_count, but it's wrong
$term_0="$term[0]";
$term_1="$term[1]";
$stm->bindParam(":term_0", $term_0);
$stm->bindParam(":term_1", $term_1);
stm->execute();
$rows = $stm->fetchALL(PDO::FETCH_ASSOC);
foreach ($rows as rows) {
$count=$rows['count'];
$term_count_number=$rows['term_count'];
$count_percentage=round(($count/$count_user_diff)*100);
$count_key_match=round(($count/$term_count_number)*100);
$combine_count=round(($count_percentage+$count_key_match)/2);
//issue is here, I want the select statement order by $combine_count
}
?>
SELECT id ,term_count, COUNT(user_id) as `count`
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY user_id
ORDER by `count` DESC");
Since "count" is a function, it would be better to put backtics around the non-function "counts", as done above.
GROUP BY should list the field not aggregated. Otherwise, it does not know which id and term_count to fetch. So, depending on what you are looking for,
Either do
SELECT user_id, COUNT(*) as `count` -- I changed this line
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY user_id
ORDER by `count` DESC");
or do
SELECT id ,term_count, COUNT(*) as `count`
FROM sign
WHERE term IN (:term_0,:term_1)
GROUP BY id ,term_count -- I changed this line
ORDER by `count` DESC");
SQL Syntax Logic
SELECT column1, count(column1) AS amount
FROM table_name
GROUP BY column1
ORDER BY amount DESC
LIMIT 12
My table structure is
I want to get the recent video uploaded of the user
What I want to do is:
"select video_id of a record who is having minimum of timestamp where userid='something'"
What I currently have is:
$recent_video = "$db->query("
SELECT video_id
FROM video_primary
ORDER BY timestamp DESC LIMIT 1
WHERE userid = '$userid'"
) or die($db->error);"`
while($row=mysqli_fetch_assoc($recent_video))
{
$video_id=$row['video_id'];
}
echo $video_id;
My table data is
How do I get the most recent view uploaded by the user?
Write WHERE clause after FROM part
Try this:
SELECT vp.video_id
FROM video_primary vp
WHERE userid='$userid'
ORDER BY vp.timestamp DESC
LIMIT 1;
try this
$recent_video=$db->query("select video_id from video_primary where userid='$userid'
ORDER BY timestamp DESC LIMIT 1 ") or die($db->error);
instead of
$recent_video="$db->query("select video_id from video_primary ORDER BY timestamp DESC
LIMIT 1 where userid='$userid'") or die($db->error);"
you are using where clause after order by and limit 1 which is a syntax error. see tutorial
Tutorial
There is a specific sequence of every clause in Query statement. Below is the sequence:
Select
Where
Order by
Limit
So, as per the above sequence, you should correct your query like below. Also, you should place double quote(") correctly.
$recent_video = $db->query("
SELECT video_id
FROM video_primary
WHERE userid = '$userid'
ORDER BY timestamp DESC LIMIT 1"
) or die($db->error);
How can I get the latest entry by the latest DATE field from a MySQL database using PHP?
The rows will not be in order of date, so I can't just take the first or last row.
You want the ORDER BY clause, and perhaps the LIMIT clause.
$query = 'SELECT * FROM `table` ORDER BY `date` DESC LIMIT 1';
SELECT * FROM [Table] ORDER BY [dateColumn] DESC
If you want only the first row:
In T-SQL:
SELECT TOP(1) * FROM [Table] ORDER BY [dateColumn] DESC
In MySQL:
SELECT * FROM `Table` ORDER BY `dateColumn` DESC LIMIT 1
You don't have a unique recordid or date revised field you could key in on? I always have at least an auto incrementing numeric field in addition to data created and date revised fields. Are you sure there is nothing you can key in on?
SELECT * FROM table ORDER BY recno DESC LIMIT 1;
or
SELECT * FROM table ORDER BY date_revised DESC LIMIT 1;
So the PHP call would be:
$result = mysql_query("SELECT * FROM table ORDER BY date_revised DESC LIMIT 1");
-- Nicholas
You can use a combination of the LIMIT and ORDER BY clauses.
For example:
SELECT * FROM entries ORDER BY timestamp DESC LIMIT 1