mysql database auto_id wrongly arranged in database - php

I don't have any coding related problem. I've a small doubt here related to mysql database. Below i've attached an image. I've inserted some records into database. For example totally 40 records i've inserted into mysql database. Some records deleted by the users. Problem is if i insert a new record (auto id is 41) its insert a record into middle of the another two rows. You can see my image below three row is there (7, 41 and 40). Why 41st record is inserted between 7 & 40 ? Why 41st record not inserted after 40th id?

SQL doesn't guarantee and order unless you specifically ask for one with an ORDER BY clause. If you want your rows in VoucherID order specify ORDER BY VoucherID in your SELECT:
SELECT * from MyTable ORDER BY VoucherID

it seems its related to sorting at your phpmyadmin view. just click on column header of "VoucherID" and check than. currently it seems sorted with "VoucherReference" (descending)

Related

checking a colum based on anther colum in mysql

I have a forum that is saved to a mysql database the table for the actual posts is call _discuss_posts
In here I have the ID of the post and another column call parent_id (there are loads more but those are the two columns that are of interest to me.)
So basically all replies and new posts are added to this table if it is a reply it has a parent_id that corresponds to the first post ID.
Now, this is where I'm getting confused. I'm trying to count the number of replies each ID has, I could do:
SELECT count(*) FROM `qwt1v_discuss_posts` WHERE `parent_id`=4968
But I don't want to manually change the parent_id each time, there is over 10k post.
So my question is this, I'm trying to figure out if there is a query I can do to automatically count based off of the ID. So for example, the first row ID is 2000 and I want to check all the other rows in the column parent_id to find the ones that are allocated to the ID 2000 once I get to the end of the table, move to the next row to grab the next ID value lets say ID value 2001 and do a search for the that in the column parent_id.
Hopefully i made some sence as its a all jumbled up in my own head so apologies if i havn't been very clear.
Right now i'm trying to simply find an SQL query but eventualy i will be moving to a php script so that i can show this in a table for some dashboards.
Thanks to whom ever reads this
Best
if you need the count of each parent_id you should use group by eg:
SELECT `parent_id`, count(*)
FROM `qwt1v_dis, uss_posts`
group by `parent_id`

PostgreSQL PHP last id values from different tables to another table

Im trying to create a table which only will have the serial and 9 int foreign keys.
Is there a way to insert the last id from other tables to this table?
i been trying with RETURNING, nextval()-1, currval(),ordering results by timestamp but nothing seems to work, i just cant get the ids values from the other tables inserted into the table.
even tried this simple set of queries for each table to update one by one of the table columns
$lst="SELECT id from table1 ORDER BY timestamp DESC limit 1";
$lst_res=pg_query($conn,$lst);
$sql_eq_bat_up="UPDATE table9 set table1_id='$lst_res[id]'";
$do_up_tbl1=pg_query($conn,$sql_eq_bat_up);
i been doing some research but had no luck, im using postgreSQL 9.5 and php for this, i hope you can help me thanks.

Limiting row count to a specific number when inserting

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);

Deleting random rows from a table without knowing anything about the table ids

I'm having a problem in mysql. I want to delete 20 rows from a table containing 100+ records.
I do not know the id's of the rows, any special identification of the rows to be deleted.
I want to just delete the any random rows from my table.
Please help me... i am new to this condition.
You can do:
DELETE FROM tbl
ORDER BY RAND()
LIMIT 20
See MySQL DELETE syntax

Alternative solution for PHP/SQL max array size problem

I have a table bundled among 100 databases in MYSQL (i.e. 1st x rows of the table in database_1, 2nd x rows of the table in database_2, ... , last x rows of the table in database_100)
Each table has a row whenever a user visits a friend for a game.
The columns are iuin, logtime, beuin.
iuin is the user id of the visitor.
beuin is the user id of the friend who was visited.
logtime is when the visit was made.
I would like to find the # of distinct friends who were visited during a week.
There is roughly 300k distinct users who are visited per day.
However, when I extended my code to calculate for a week, I ran out of memory.
My code basically does an SQL query using SELECT DISTINCT beuin for a selected week for the table in each database. I store all the beuin in an array if it's not already stored (so I count distinct friends who were visited), and return the size of the array at the end.
FYI, I can't edit the database around such as joining all the tables in different databases into one table.
Is there any alternative ways i can do this?
Thanks
It's hard to say anything about your without the one. But I think you can solve this problem using mysql. My quick solution:
Create table - CREATE table if not exist users_ids(user_id INT NOT NULL DEAULT 0 PRIMARY KEY(UNIQUE)); in the first db
Truncate users_ids
Run 100 queries like INSERT IGNORE INTO db1.users_ids select distinct user_id from db1.table1;
Select count(*) from users_ids;

Categories