Find single entries where there should be 2 - mysql

I am looking to find all the single entries in a table where there should only be double entries.
Eg.
Unique_Key
ID
State_Sequence_ID
Localisation_Format_ID
File_Name
6644106
1315865
100
1
2064430-DNK.pac
6644107
1315865
190
2
2064430.chk [DNK]
I am looking to find all instances where the 2nd record does not exist.
The ID for each record will always be the same (although I do not know what that ID will be specifically) and the Localisation Format ID will always be 1 and 2. I am looking to find all entries where Localisation Format ID 2 does not exist.
SELECT *
WHERE ID has Localisation_Format_ID = 1
but does not have Localisation_Format_ID = 2

This is a simple not exists criteria:
select *
from t
where not exists (
select * from t t2 where t2.Id = t.Id and t2.Localisation_Format_ID = 2
);

Related

How can I copy rows from one to another table with a different colnm data

I had two tables Table 1 & Table 2 AS shown here
Table:1
ID
IMG_PATH
CAT_ID
166
hfhbf
1
164
jgj
2
162
ggd
1
160
mfnf
1
158
dbd
2
Table:2
ID
IMG_PARENT_ID
Here I want to print table 1's ID column data Example:166
Here (ID-1) Example:165
Here I want to print table 1's ID column data Example:164
Here (ID-1) Example:163
Here I want to print table 1's ID column data Example:162
Here (ID-1) Example:161
Here I want to print table 1's ID column data Example:160
Here (ID-1) Example:159
Here I want to print table 1's ID column data Example:158
Here (ID-1) Example:157
AS SHOWN IN TABLE 2 I NEED FOLLOWING VALUE...
and dont try this manually method:
INSERT INTO tabla2
SELECT * FROM tabla1
WHERE id = 1 //Here we write the condition
I want to fetch data because arround 10,000's row are inserted in this table
Lots of tries but didnt get it
based on what you provided info about your question, this is what I understand about this.
Assuming that table 1 is auto_increment with ID of 1-10,000.
Then you can use this to select the even IDs in table 1 and insert it to table 2
insert into table2 (ID) select ID from table1 group by ID having mod(ID, 2) = 0;
To select odd IDs from table 1 and insert it to table 2 you can use this
insert into table2 (IMG_PARENT_ID) select ID from table1 group by ID having mod(ID, 2) = 1;

Show records where a value exist in all instances of a field by group

I am trying to figure out a way to show all records in table where a specific field does not contain certain values - table layout is:
id
tenant_id
request_action
request_id
request_status
hash
Each request_id could have multiple actions so it could look like:
1 1 email 1234 1 ffffd9b00cf893297ab737243c2b921c
2 1 email 1234 0 ffffd9b00cf893297ab737243c2b921c
3 1 email 1234 0 ffffd9b00cf893297ab737243c2b921c
4 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
5 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
6 1 email 1235 1 a50ee458c9878190c24cdf218c4ac904
7 1 email 1236 1 58c2869bc4cc38acc03038c7bef14023
8 1 email 1236 2 58c2869bc4cc38acc03038c7bef14023
9 1 email 1236 2 58c2869bc4cc38acc03038c7bef14023
Request_id can either be 0 (pending), 1 (sent) or 2 (failed) - I want to find all hashes where all the request_status within that hash are set to 1.
In the above two examples a50ee458c9878190c24cdf218c4ac904 should return as a match as all the request_status are 1 but ffffd9b00cf893297ab737243c2b921c should not as, whilst it contains a 1, it also contains some 0's and 58c2869bc4cc38acc03038c7bef14023 should not as, again whilst it contains a 1, it also contains some 2's
I tried:
SELECT
*
from
table
where request_action='email' and request_status!=0 and request_status!=2
group by hash
However, this doesn't give me the result I need - how can I return the hashes only where request_status is set to 1 for all the instances of that hash?
Not sure why you would need a group by here. You'd want to do a group by if you were going to concat data using GROUP_CONCAT, or other aggregate functions (sum, max, etc)
Also, instead of doing multiple negative conditions in your where clause (request_status !=0 and request_status !=2), why not just get the status you want?
SELECT * FROM test WHERE request_action = 'email' AND request_status = 1
Update Based on Your Comment
If you don't want to return any hashes that have the status of 0, or 2. You can do this:
SELECT
*
FROM
test t
WHERE
request_action = 'email' AND request_status = 1
AND HASH NOT IN (SELECT HASH FROM test WHERE request_status IN (0, 2))
Just make sure you have an index on hash, otherwise this is going to be really slow.
Create table temp select hash from your_table where
request_status=1 group by hash
Alter table temp add index(hash)
Delete from temp where hash IN (select hash from temp
where request_status!=1 group by hash)
Select * from your_table where hash IN(select hash from
temp)

MySQL Query: column BETWEEN A AND B OR C

I wondered if I can get a query like this to work:
"DELETE FROM table WHERE id (BETWEEN 1 AND 5) OR = 8;"
The thing I want to achieve: I want to say; get me the results between some values or also the value if my id = 8.
Unfortunately I only found queries and questions how to search for
things like this.
"DELETE FROM table WHERE id BETWEEN 1 AND 5 AND `email`='sarah#sarah.com';"
Or would I have to do it likewise:
"DELETE FROM table WHERE id BETWEEN 1 AND 5 OR id = 8;"
Thanks for a short input on that...
Cheers
To DELETE:
DELETE FROM table WHERE (id BETWEEN 1 AND 5) OR id = 8
To SELECT:
SELECT * FROM table WHERE (id BETWEEN 1 AND 5) OR id = 8

How to delete a row where there are only one of the kind in MySql?

I have following data in MySQL table named info:
chapter | section
3 | 0
3 | 1
3 | 2
3 | 3
4 | 0
5 | 0
I would like to delete a row for chapter = n, but only when there is no section>0 for same chapter. So chapter 3 can't be deleted while chapter 4 and 5 can. I know the following doesn't work:
DELETE info WHERE chapter = 3 AND NOT EXISTS (SELECT * FROM info WHERE chapter = 3 AND section>0);
The same table is used twice in the statement. So what is the easiest way to achieve my goal?
You've got the idea right. Here is the syntax:
DELETE
FROM mytable
WHERE chapter NOT IN (
SELECT * FROM (
select tt.chapter
from mytable tt
where tt.section <> 0
group by tt.chapter
) tmp
)
The nested select is a workaround a bug in MySQL.
Demo.
You can run a sub query to return the rows that have sections of more then one and then delete the rows returned from the sub query.
DELETE FROM table1 WHERE table1.chapter Not IN (select chapter from
(SELECT table1.chapter FROM table1 WHERE Table1.section >=1 ) Results);
Example Fiddle based on your question
You could also supply the chapter as well in the sub query where clause if you only want to delete a specfic chapter. If it does not meet the where clause then no records will be deleted.
This should do it.
DELETE FROM Table t
WHERE NOT EXISTS(
SELECT 1
FROM Table t2
Where t2.chapter = t.chapter
And t2.section > 0
)
In my experience Exists generally performs better than In. If you are storing a large amount of records you should take this into consideration.

mySQL count occurances of value on multiple fields. How?

I have a table with 5 fields. Each field can store a number from 1 - 59.
Similar to countif in Excel, how do I count the number of times a number from 1 - 59 shows up in all 5 fields?
Here's an example for the count of occurances for the number 1 in all five fields:
SELECT SUM(pick_1 = 1 OR pick_2 = 1 OR pick_3 = 1 OR pick_4 = 1 OR pick_5 = 1) AS total_count_1
FROM tbldraw
Hopefully I made sense.
There was an answer here that had a solution. I think this is just a variation.
Step1: Create a numbers table (1 field, called id, 59 records (values 1 -59))
Step2:
SELECT numbers_table.number as number
, COUNT(tbldraw.pk_record)
FROM numbers_table
LEFT JOIN tbldraw
ON numbers_table.number = tbldraw.pick_1
OR numbers_table.number = tbldraw.pick_2
OR numbers_table.number = tbldraw.pick_3
OR numbers_table.number = tbldraw.pick_4
OR numbers_table.number = tbldraw.pick_5
GROUP BY number
ORDER BY number
How about a two step process? Assuming a table called summary_table ( int id, int ttl), for each number you care about...
insert into summary_table values (1,
(select count(*)
from table
where field1 = 1 or field2 = 1 or field3 = 1 or field4 = 1 or field5 = 1))
do that 59 times, once for each value. You can use a loop in most cases. Then you can select from the summary_table
select *
from summary_table
order by id
That will do it. I leave the coversion of this SQL into a stored procedure for those that know what database is in use.
The ALL() function, which returns true if the preceding operator is true for all parameters, makes the query particularly elegant and succinct.
To find the count a particular number (eg 3):
select count(*)
from tbldraw
where 3 = all (pick_1, pick_2, pick_3, pick_4, pick_5)
To find the count of all such numbers:
select pick_1, count(*)
from tbldraw
where pick_1 = all (pick_2, pick_3, pick_4, pick_5)
group by pick_1