This question already has an answer here:
Find missing sequence gaps mysql [duplicate]
(1 answer)
Closed 8 years ago.
I'm using MySql to keep track of images, each image is assigned an auto increment ID. The ID column should look in theory similar too,
(1,2,3,4,5,6)
Sadly, over the past two years I've been forced to delete rows due to moderation reasons and my ID column looks more along the lines of,
(1,3,4,7)
Is there anyway to get the missing IDs, i.e.:
(2,5,6)
I've search around here and various other forums, but alas I've come up with nothing. The only answer that i've found similar to mine is comparing my ID column to another table's and finding the differences. This is an option, but my main table consists of 25,000 rows so it would be a bit difficult to do.
Thank you in advance!
Create temporary table with integer from 1 to 25000. Then do a left/right join on it to find the missing IDs.
Try that:
SELECT a.id+1 AS start, MIN(b.id) - 1 AS end
FROM table1 AS a, table1 AS b
WHERE a.id < b.id
GROUP BY a.id
HAVING start < MIN(b.id) ;
DEMO HERE
Related
This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 4 years ago.
I have an issue with SQL IN query: I'm storing multiple employee IDs in a table, separated with commas for each task. When I try to fetch a task with an IN query, I'm not getting the row which contains the employee IDs.
My query is:
select
t.*,
e.emp_name,
d.department_name
from
task as t
LEFT JOIN employee as e on(e.emp_id=t.assign_to)
LEFT JOIN department as d on(d.depart_id=e.depart_id)
where
t.task_status='PENDING'
AND t.created_by!='31'
AND t.assign_to IN ('31')
order by
t.task_id DESC
The stored value in database
IN doesn't work like that
Example if your data looks like:
ManagerName, ManagerOf
'John', 'Steve,Sarah,Dave'
You can't do:
SELECT * FROM managers WHERE 'sarah' IN ManagerOf
IN is best conceived as an extension of OR:
SELECT * FROM managers WHERE managerof IN ('Sarah','Steve')
--is the same as:
SELECT * FROM managers WHERE
managerof = 'Sarah' OR
managerof = 'Steve'
There would be as many OR clauses as there are items in the IN list.
Hopefully this shows you why the database doesn't return you any results.. Because a value of Steve,Sarah,Dave is not equal to either Sarah or Steve. The database doesn't look at the commas and say "oh, that's a list" - it's just a single string of characters that happens to have a comma character every now and then
There are nasty quick hacks to so-so achieve what you want, using LIKE and string concat but they aren't worthy of an answer, to be honest
You need to change your database structure to include a new table that tracks the task id and the employee(s) id it is/was assigned to. After doing that, you'll be able to use IN on the employee id column as you're expecting to with this query
This question already has answers here:
Delete duplicated records from a table without pk or id or unique columns in mysql
(4 answers)
Closed 7 years ago.
EDIT: I added a response with the link and command that solved my issue. To put it straight for any future readers desperate about their unindexed tables with duplicate rows, you'll find the answer bellow or in the link above.
I have a little screwed up table in my db. It was repeated row with the exact same information (including id).
Is there a query I can use to delete these repeated id's but one?
I wanted to check my answer here first, if I delete this table it's going to take me the whole weekend to import it again (it's huge)...
I'm thinking of doing
DELETE FROM tickets t1, tickets t2 WHERE t1.id = t2.id limit 1;
I'm not sure this will work...
Thanks!
As Oceans says
CREATE TEMPORARY TABLE tmp_tickets AS SELECT DISTINCT * FROM tickets;
DELETE FROM tickets;
INSERT INTO tickets SELECT * FROM tmp_tickets;
DROP TABLE tmp_tickets;
To find all the duplicates in the id column you could try:
select `id`
from `tickets`
group by `id`
having count(`id`)>1;
I would try this in a gui ( Heidi etc ) ~ you can then base your delete operations around this, either manually in the gui or by direct query.
I managed to fix this with the solution found here Remove duplicate rows in MySQL. To save you clicking the link the query looked something like this
ALTER IGNORE TABLE jobs
ADD UNIQUE INDEX idx_name (site_id, title, company);
It removed all the repeated indexes, minus one.
This question already has answers here:
MySQL: Fastest way to count number of rows
(14 answers)
Closed 8 years ago.
I want to count all of rows in table that match to my condition as fast as mysql can do.
So, i have four SQL and want you to explain all of them, how is it different for each SQL?
and which is fastest or best for query times and server performance? or it has another way that can better than these. Thank you.
select count(*) as total from table where my_condition
select count(1) as total from table where my_condition
select count(primary_key) as total from table where my_condition
or
select * from table where my_condition
//and then use mysql_num_rows()
First of all don't even think about doing the last one! It literally means select all the data I have in the database, return it to the client and the client will count them!
Second, you need to understand that if you're counting by column name, count will not return null values, for example: select count(column1) from table_name; if there is a row where column1 is null, it will not count it, so be careful.
select count(*), select count(1), select count(primary_key) are all equal and you'll see no difference whatsoever.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to request a random row in SQL?
I have table:
name,age,school
jane,15,zu
peter,16,zu
john,15,stu
Tomas,15,kul
viera,17,stu
tibor15,zu
I want select from this table 1 person (randomly) per school
select * from table group by school order by rand()
It's terribly innefficient since it's ORDERing the entire table randomly, and then GROUPing on a potentially huge unindexed temporary table, but this works.
SELECT *
FROM (
SELECT *
FROM my_table
ORDER BY RAND()
)a
GROUP BY school
It would likely be more wise to break it down.
One query to retrieve a list of
schools.
For each school, one query to get a random student for that school.
See this post for some slick tricks on how to get single random values efficiently.
If you are using php (as the tag suggests), run SELECT * FROM table; then generate a random number based on the number of results in the query. For example:
i = floor(random()*query.length);
Then seek to the record indicated seek(i) and viola, you have a random entry =)
You'll have to use your own knowledge/documentation for the exact syntax, I haven't touched PHP in a while, but the logic is sound.
Gary
I have working in database Postgresql for the first time. I need your help to finding out a solution. One table contains 15 rows With a regn_srno as P.K., another table has the same regn_srno as F.K. I want to count the total number of rows which has the same regn_srno. But My problem is the second table has contain 2 or 3 fields with the same regn_srno. So when i use count in the query it shows 12 (including the same regn_srno), but the original number is 10. Due to the same regn_srno repeat in the second table i got the answer as 12.
When we group by regn_srno we get the result as 1,1,1,1,2,1,2,1,1,1. So i need the query to get the count as 10.Please help me. Please send me the answer through my mail id.
For what I could figure out without tables schema, I think you want
SELECT count(DISTINCT regn_smo) FROM t1 JOIN t2 USING (regn_smo);
You could simply do:
SELECT count(DISTINCT regn_smo) FROM t2