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

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;

Related

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

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)

How to generate values in dataset

I have the dataset in a text file in below format:
user ID Song ID Rating
0 7171 5
0 8637 4
0 21966 4
0 35821 5
(It has no dashes between just a TAB space between them)
I have about 1 million records like this. I need to generate a fourth record that has genre ID. This genre ID I have it in another text file like in below format:
Song ID Album ID Artist ID Genre ID
4 243 2282 0
5 7783 3832 0
6 11704 1655 106
7 10126 6328 114
8 10672 4121 0
How to generate the "genre ID" for the corresponding "song ID" in the dataset.
Can you please suggest an easiest way to generate the fourth column.
# Anders Finn : He's using MySQL, It doesn't support SELECT ....INTO(Oracle)
INSERT INTO newTable
SELECT tb1.userID, tb1.SongID, tb1.Rating, tb2.GenreID
FROM tab1
LEFT JOIN tab2
ON tab1.SongID = tab2.SongID
I would've added it as a comment, but I still can't.
Create a new table with all four columns:
CREATE TABLE newTable
(
userid int,
SongID int,
Rating int,
GenreID int
)
The populate it:
SELECT tb1.userID, tb1.SongID, tb1.Rating, tb2.GenreID
INTO newTable
FROM tab1
LEFT JOIN tab2
ON tab1.SongID = tab2.SongID
Below
it's said that the syntax doesnt apply for mySQL and only for Oracle. the syntax fra mysql are described here: http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
In this case the result would be:
INSERT INTO newTable (userID, SongID, Rating, GenreID)
SELECT tb1.userID, tb1.SongID, tb1.Rating, tb2.GenreID
FROM tab1
LEFT JOIN tab2
ON tab1.SongID = tab2.SongID
If there is many millions records it would be an idea to create an index on SONGID on tab1 and tab2.

Dragging some rows in the middle of the table

So as i say , is possible copy each row and put it on the row below or as the same move field rows to the fields below? ( have and index auto increment of course ).The result should be the old row 40 content (fields) in the row 41 and so.
REPLACE table SET column1 = previuos(column1), column2= previous(column2), column3=(previous(column3) where id > 20 and id < 300
They are not the last inserted, they are in the middle of the table and want to move it down , so that let me some empty rows ( with the index ) at the beginning of the selected ones
INSERT INTO table (
SELECT column1,column2,column3
FROM table WHERE id > 20 AND id < 200
) WHERE id = 30
selected row from 21 to 199 then inserted on 30 and ahead
thank you
Your question is kind of hard to understand, but if you want to duplicate the last inserted row, you could do something like that :
INSERT INTO your_table (
SELECT NULL, column_1, column_2, column_etc
FROM your_table ORDER BY your_id DESC LIMIT 1
);
Remember that a database table is not a spreadsheet.
However this will do what you seem to want:
UPDATE mytable
SET id = id + 9
WHERE id BETWEEN 21 AND 199;

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