mysql check userID of last x posts - php

I have a php chat script on my site that I made, and I want to add in an anti-spam measure so that it wont post your message if the last 5 messages in the mysql table are by you.
Do I have to cycle through the last 5 with a recordset or is there a SQL statement that can just check this for me?
the table fields are just 'date' 'text' 'userid'

You can use a query like this to get how many of the last 5 messages where by the person in question.
select
sum(if(userid = '$user_id',1,0)) = 5
from (
select userid from chat order by id desc limit 5
);

In MySQL:
select userid, count(date) as howmany from
(
select userid, date
from YOURTABLENAME
order by date desc
limit 5
)
lastfiverows
group by userid
order by howmany
Then in PHP check to see if the "howmany" field is five, and the userid matches the user in question.

Order the record set (ORDER BY) by date (DESC) and LIMIT it to 5

Related

MYSQL: how to retrieve all table rows + only the last row where column equals

OK, a bit of a complicated query, for me anyway.
I'm trying to build a MYSQL query to retrieve all the rows from a DB, but I only want the last row where the column 'topic' equals 'This Week'.
So, what I mean is this:
I have a DB table with all my blog articles. Every week, I write a summary of the aviation news from the past week. I now want to show all the blog posts on the index page, but I also only want to show the last 'This Week' post.
This is the query I use now to retrieve all the rows:
SELECT * FROM articles WHERE published = '1' ORDER BY date_time DESC LIMIT 10
So I need a query that returns all the rows from my table 'articles' + only the last row where 'topic' = 'This Week'.
Anyone who can help me with this?
you may use Unions ( something like this
SELECT * FROM articles WHERE published = '1' ORDER BY date_time DESC LIMIT 10
UNION
SELECT * FROM articles WHERE WHERE topic="This Week" order by ID desc LIMIT 1
you may use UNION or UNION ALL whichever suits your need
you may wanna check the actual query and format it as per your needs

How to get the last inserted row when I Group by column in mySQL?

I run this query to get me the active users in a duration of 10 minutes. the clicks table has multiple rows of a user with each page. I can get the list of user but the page next to the output is not the last row, but the first.
That means if 4 rows are stored for a specific user, 1st is 1 minute ago, and the 4th is 8 minutes ago, it will show me the 8th minute page, not the 1st minute page.
How to fix this ?
SELECT user
, page
FROM clicks
WHERE timestamp >= NOW() - INTERVAL 10 MINUTE
GROUP
BY user
ORDER
BY id DESC
This will work for all versions
SELECT
id, tc_stage_id, user
FROM
clicks
WHERE
id IN (SELECT
MAX(id)
FROM
clicks
GROUP BY user)
Try this:
select temp.*
from
(
select user , page
from clicks
order by timestamp desc
) temp
group by temp.user;
mysql_insert_id
Retrieves the ID generated for an AUTO_INCREMENT column by the previous query

Update a set of records based on count - MySQL

I have a 100 active records in my table - cars. The user has decided to downgrade their account and is now eligible to only have 5 active records. I need to set the expiration date on 95 records (oldest first) to current timestamp. How do I do this in MySQL - bonus thanks if you include some cakephp hints (but I will thank you for just the SQL hint alone)
Here is what I tried but I get an error message stating that mySQL does not support LIMIT in sub-query
UPDATE cars
SET archived = NOW()
WHERE id NOT IN (
SELECT id
FROM cars
ORDER BY created DESC LIMIT 5
)
The MySQL documentation does not explain why LIMIT clauses in this type of subquery is not supported. However, there is no such limitation on sub-subqueries.
To resolve this issue, lets create another subquery and move the LIMIT clause into it:
WHERE id NOT IN (
SELECT id
FROM (
SELECT id
FROM cars
ORDER BY created
DESC LIMIT 5
)
AS carsInner
)
The innermost query will select only the last five rows, and exists only to satisfy the requirement that the subquery should not contain the limit clause.
The final query looks like:
UPDATE cars
SET `archived` = NOW()
WHERE id NOT IN (
SELECT id
FROM (
SELECT id
FROM cars
ORDER BY created
DESC LIMIT 5
)
AS carsInner
);
See an SQL Fiddle demo here.

Last results submitted in mysql db

How would i get the last 20 results submitted in a mysql db row.
I need the 20 most recent user submitted results.
Cheers.
If you don't have timestamp or autoincrement field on that table then you can't.
Otherwise : select * from table order by id desc limit 0,20
If you are using an auto_increment primary key, its as simple as:
SELECT * FROM that_table
ORDER BY auto_increment_primary_key DESC
LIMIT 0, 20
See the Query below, there is an function available in mysql syntax below
SELECT FROM ORDER BY DESC LIMIT 0,20

Making MySQL return the table backwards

I want to make a simple news system using PHP and MySQL, right now I got a working post and read system but there is only one problem, I want it to show the 10 latest news but instead it shows the 10 oldest news.
My question is: Is there a way to make MySQL return the results from the bottom of a table or do I have to first get the number of posts and then limit it to the very last 10 ones?
Here is the insert (title and text is escaped and time is time(), poster is not done yet):
mysql_query("INSERT INTO news (title, poster, text, time) VALUES ('$newstitle', '1', '$newstext', '$time')") or die(mysql_error());
And to retrive it (addnews echos it):
$myqr = mysql_query('SELECT * FROM news LIMIT 10') or die("Error running news query: ". mysql_error());
while($myres = mysql_fetch_array($myqr))
{
addnews($myres['id'], $myres['title'], "admin", date('l jS F Y - H:i:s', $myres['time']), $myres['text']);
}
So, short: I want to read the database backwards, is it possible?
Check out the ORDER BY clause. It allows you to sort rows by a column in ascending or descending order. The following query will return 10 news items, sorted by time in descending order.
SELECT * FROM news ORDER BY time DESC LIMIT 10
Simple, just add an "ORDER BY" clause to your SQL, e.g.
SELECT * FROM news ORDER BY time DESC LIMIT 10
you need to modify your query to sort by the date it was created. something like
SELECT * FROM news order by time DESC LIMIT 10
should work for you. I think its worth noting that if you do not specify an Order by clause, the order in which results are returned is not guaranteed. Right now, you happen to be getting them ordered by the time they were inserted ascending, however you cannot safely assume that will always be the case.
Assuming that id is the primary key:
SELECT * FROM new ORDER BY id DESC LIMIT 10
Use an ORDER BY clause.
Could you not simply Order By time Descending before LIMIT 10?
use
SELECT * FROM news ORDER BY time DESC LIMIT 10
You could also use
SELECT TOP 10 FROM news ORDER BY time DESC
I believe. Not sure if the 'top' clause is SQL Server only, though.

Categories