MySQL Query: column BETWEEN A AND B OR C - mysql

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

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;

Find single entries where there should be 2

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

Update max value from a mysql table

I'm trying to update a table's value using max(col) in a subquery, but for some reason, it's updating all values that match the user_id column I'm using.
This is my table structure:
user_id | steps | in_date
--------+-------+-----------
8 |10 | 1522246892
8 |10 | 1522250713
7 |10 | 1522250799
And this is my query:
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
AND in_date = (SELECT max(in_date) WHERE user_id = 8);
I expected it to update only the second column, but it instead updates both columns with user_id = 8. Why isn't it working as expected? What am I doing wrong?
Edit: Thanks to Manoj's comment, I changed the query to the following, and it works:
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
ORDER BY in_date DESC LIMIT 1;
Doing it his way is even better, since I don't have to run two queries, and already gets the highest one by id.
You will encounter sql error "You can't specify target table 'userdata' for update in FROM clause" when you use same table in the sub-query to update the same table.
Its strange that you say its running because,
- you missed from clause in your sub-query
- you can't use same table
Can you please be more specific.
seems you missed the from clause in subselect and for avoid conflit between select and updated value you could build a temp table using ainner subselect
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
AND in_date = ( select max_date from (
SELECT max(in_date) max_date FROM userdata WHERE user_id = 8) t);
and in this way the AND clause could be always true

How to Increment the Table Value With Max ID from Another Table

I have a table like this:
Tb_server
ID
5
6
7
Tb_upload
ID
1
2
3
I need a query which can update all the ID of Tb_server to max(Tb_upload.ID) + 1
So, the result on the Tb_server should be like this
Tb_server
ID
4
5
6
I am doing this in a shell script, so I can get the max(Tb_upload.ID) as a variable.
But what will be the query, using MySQL?
Try this:
UPDATE Tb_server, (SELECT #auto:=0) a
SET ID = (SELECT MAX(ID) FROM Tb_upload) + (#auto:= #auto+1);
Take the max+1 of Tb_upload which is : 4
Make the update as :
update Tb_server set Tb_serverID= (Tb_serverID+4); // 4 is the difference here

mysql update between row and shift current to right

how to update column value of specific id and shift after to right.
id track
1 3
2 5
3 8
4 9
want to update id 3 track column value to 10, result like this
id track
1 3
2 5
3 10
4 8
5 9
id column is auto_increment
or any suggestion it's my pleasure.
thank you.
You should avoid tweaking auto_increments. Auto increment keys are usually supposed to be used internally (e.g. for linking purposes). If you want to order tracks, i suggest you add a seperate numeric field "ordernro" to the table and update that
To add a column order nro to a table named album, do like this:
alter table album add ordernro int(2) after id;
Then copy the current value for id into this new column:
update album set ordernro=id;
(do this only once after adding the column)
To insert track 10 at position 3 first shift the rows:
update album set ordernro = ordernro + 1 where ordernro >= 3;
And then insert track 10:
insert into album (ordernro, track) values (3, 10);
Remember to update your existing insert/update/select statements accordingly.
The result can be checked by:
select * from album order by ordernro;
(The id will now be "mixed up", but that doesn't matter)
UPDATE table SET id = id + 1 WHERE id >= x;
x being the id where you place your current track.
The problem with JK 's answer is that MySQL returns error saying that is can't UPDATE because the index at x+1 would be duplicate.
What I did is
UPDATE table SET id = id + 100 WHERE id >= x;
UPDATE table SET id = id - 99 WHERE id >= x;
And then INSERT my row at index x