This might sound quite stupid, but whats the best approach to count a users db entries of today, so I can publish the most activate member with mysqli?
Thanks..
Edited; Current code
$promote_amount_posts = mysqli_query($mysqli,"SELECT COUNT(rec_by_id) FROM posts LIMIT 1 ");
$row = mysqli_fetch_assoc($promote_amount_posts);
$promote_amount_posts = $row['COUNT(rec_by_id)'];
$most_active_user_id = $row['rec_by_id'];
Lets say my user_id (which goes into the rec_by_id column) is 82392 and I created 20 posts. There should be atlease MY 20 posts in the 'posts' table. How do I fetch my user_id so I can echo it anywhere?
The following query should return the count in today_db_entries as long as you have a created column and a id column - swap out those names for whatever your schema uses.
SELECT COUNT(id) as today_db_entries
FROM table
WHERE created BETWEEN getdate() AND getdate()-1
Related
My professor wants us to create a web-based comment system wherein a user can send up to 3 comments, if the user decides to create another one when he already has 3 comments named after him on the database, the program should delete the oldest one and save the new one.
What I thought about was to fetch the rows named after the user and if it is greater or equal to 4, I should delete the row where username = session user and insert the new record. Although this was just in theory, is this the best way to go about this? Do you guys have any other suggestions? How exactly do I pick the rows to remove? Do I base it off the highest comment_id?
Read: Leave only first 50 records in SQL database and delete the rest
So basically do this:
Create an Auto Increment Id with the comments.
Then:
DELETE FROM comments
WHERE
id NOT IN (
SELECT * FROM (
SELECT id
FROM comments
ORDER BY date
desc LIMIT 3) s
)
Do not delete the rows in the database, just do a limit 0,4 on your select query
with a order by on a create date
I have a table with data relating to a user, and two important columns:
refer_count, which is updated when a new entry is made in the table with the referred_by column set to that users user_id, and referred_by which is the user_id of the of the user that referred them.
I want to select the users from the table that have the highest number of referrals after a certain date.
For example:
If there are 3 users, one of which referred the other 2 (lets say users 2 and 3), however user 2 was referred on the 2/12/14, whereas user 3 was referred on the 3/1/15.
If the cutoff is 1/12/14, then user 1 is returned with refer_count set to 2, but if the cutoff is after 2/12/14, then user 1 is returned with refer_count set to 1.
I've been thinking of how to do this, but I can't think of a way that would work. Is there a way?
This is via MySQL.
EDIT: I think I may need to provide for information.
The date registered (register_date) is used as the refer date. I need the refer_count to be updated with the number of users referred after the cutoff, however I need to get the actual user. This is for a 'top referrers' table. I can't figure out why I'm having so much trouble thinking of a way to do this.
SELECT user_id FROM usertable WHERE (referal_date BETWEEN '2014-12-2' AND CURDATE())ORDER BY refer_count DESC;
That's the rough idea.
You should look into normalizing your tables if you're keeping that all in the same table, though. It'd be better to keep referals in a seperate table.
Get the row with the maximum in refer_count with a Date condition for your referal_date such that it's after the certainDate:
SELECT user_id FROM table WHERE refer_count = (SELECT MAX(refer_count) FROM table) AND referal_date>certainDate;
Note that WHERE is before SELECT so it will not get the highest count first, but will filter with the date condition then get the highest count.
Edit: Updated query based on edited question.
I'm thinking of implement a view history for my wordpress blog, where users can view their previously viewed articles as a list in their account page.
I would like to limit this to 24 unique page history per user at any point of time, meaning, if the number of articles exceeds 24, the oldest article row would be deleted, and the new article added to the table.
I'm using PHP and MySQL.
Here's my current thoughts on implementation:
Create a table with user_id and post_id columns
When user views an article, insert new row into the table
Select the rows with the current user_id, and if number of rows is more than 24,
Delete the oldest row
I'm not sure if this is the best method, since it's 3 additional database queries per user page view which is pretty heavy.
Is there a better way to do this?
The ideea is good. Improve it by updating the oldest row instead of deleting it and then adding a new one. ;)
Also make a single read query and a single write query.
make a query that is like this one
SELECT (SELECT COUNT(*) FROM recentArticles WHERE userID = 23)
AS NoOfArticles, articleID, timestamp
FROM recentArticles
WHERE userID = 23
ORDER BY timestamp ASC
LIMIT 1;
If NoOfArticles < 24 then execute an insert query, else execute an update query to articleID
You could always implement this using cookies to key on which pages have been visited and keeping a running list on the users PC. This will reduce the traffic to the site, put the processing scripts on the user-side, and could be easier to implement.
That being said, I agree with the other answer about updating the oldest entry if the table is full. This eliminates the need to delete and add a row. Key off a time-date stamp to sort the entries when you're displaying them and to figure out which page was the oldest (and needs to be updated if >= 24 pages.
You can merge two queries in one. In this way, You will save execution time of your script. So, basically, DELETE row if the post count is more than 24. You can modify this query according to your exact need. But yes, you can think on this way.
DELETE FROM `table_name`
WHERE id=(SELECT (CASE WHEN COUNT(id)>24 then id END)
AS last_id
FROM `table_name`
WHERE user_id='XX'
ORDER BY id DESC LIMIT 1);
I want to be able to update a row with the highest ID.
The problem is: I can't find any elegant solution to do this.
This is my best attempt so far:
$highestId = mysql_result(mysql_query('SELECT MAX(id) FROM stats'),0);
mysql_query("UPDATE stats SET views = views +1 WHERE id = $highestId");
Maybe there there is a better approach than I am thinking of.
I am tracking the amount of views, every day
I want it to auto-increment the last (highest id) day
In the evening I'm running a cronjob that creates a new day.
Any suggestion on how to tackle this problem are welcome, even if it is a whole different approach.
Table stats => id | views
Yes:
UPDATE stats SET views = views +1 ORDER BY id DESC LIMIT 1
You can use #dev-null-dweller version if you don't suffer from table ordering. Or you can use a subquery.
UPDATE stats SET views = views +1 WHERE id = (SELECT * FROM (SELECT MAX(id) FROM stats) id)
You can profile both solutions and see which one works best for your case.
I think the following would be the best solution for you to track the stats, does not require a cronjob.
Create a table with two columns
Table: stats
Columns: stat_date (DATE) PRIMARY, views (INT)
Then run the query:
$query = "INSERT INTO stats(stat_date, views) VALUES('".date('Y-m-d')."', 1) ".
'ON DUPLICATE KEY UPDATE views = views + 1';
Edit: I previously suggested a DATETIME type for the stat_date column, but it's obvious that a DATETIME doesn't make sense for you, since you want only a record for a day not a second. Thus, I substituted the DATETIME type for DATE.
You could also sort results by id desc and just edit the first result.
Edit: Too late sorry. :)
To randomly select records from one table; do I have to always set a temporary variable in PHP? I need some help with selecting random rows within a CodeIgniter model, and then display three different ones in a view every time my homepage is viewed. Does anyone have any thoughts on how to solve this issue? Thanks in advance!
If you don't have a ton of rows, you can simply:
SELECT * FROM myTable ORDER BY RAND() LIMIT 3;
If you have many rows, this will get slow, but for smaller data sets it will work fine.
As Steve Michel mentions in his answer, this method can get very ugly for large tables. His suggestion is a good place to jump off from. If you know the approximate maximum integer PK on the table, you can do something like generating a random number between one and your max PK value, then grab random rows one at a time like:
$q="SELECT * FROM table WHERE id >= {$myRandomValue}";
$row = $db->fetchOne($q); //or whatever CI's interface to grab a single is like
Of course, if you need 3 random rows, you'll have three queries here, but as they're entirely on the PK, they'll be fast(er than randomizing the whole table).
I would do something like:
SELECT * FROM table ORDER BY RAND() LIMIT 1;
This will put the data in a random order and then return only the first row from that random order.
I have this piece of code in production to get a random quote. Using MySQL's RAND function was super slow. Even with 100 quotes in the database, I was noticing a lag time on the website. With this, there was no lag at all.
$result = mysql_query('SELECT COUNT(*) FROM quotes');
$count = mysql_fetch_row($result);
$id = rand(1, $count[0]);
$result = mysql_query("SELECT author, quote FROM quotes WHERE id=$id");
you need a query like this:
SELECT *
FROM tablename
WHERE somefield='something'
ORDER BY RAND() LIMIT 3
It is taken from the second result of
http://www.google.com/search?q=mysql+random
and it should work ;)
Ordering a big table by rand() can be very expensive if the table is very large. MySQL will need to build a temporary table and sort it. If you have primary key and you know how many rows are in the table, use LIMIT x,1 to grab a random row, where x is the number of the row you want to get.