![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table SELECT NULL, NULL, var2, var3 FROM table WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason |
#3
| |||
| |||
|
|
I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table * SELECT NULL, NULL, var2, var3 * FROM table * WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL AND NOT EXISTS (SELECT var1 * * * * * * * *FROM table * * * * * * * *WHERE var1="something_else" ); Maybe you have to change this subquery to match your exact needs..... -- Luuk |
#4
| |||
| |||
|
|
I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table SELECT NULL, NULL, var2, var3 FROM table WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason Is there a unique index that would prevent the duplicates? If so, just |
#5
| |||
| |||
|
|
On 10/09/11 23:16, jwcarlton wrote: I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table * SELECT NULL, NULL, var2, var3 * FROM table * WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason Is there a unique index that would prevent the duplicates? If so, just use the INSERT .. ON DUPLICATE KEY construct. Good luck, -- Willem Bogaerts Application smith Kratz B.V.http://www.kratz.nl/ |
#6
| |||
| |||
|
|
On Sep 12, 3:44 am, Willem Bogaerts<w.bogae... (AT) kratz (DOT) nl> wrote: On 10/09/11 23:16, jwcarlton wrote: I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table SELECT NULL, NULL, var2, var3 FROM table WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason Is there a unique index that would prevent the duplicates? If so, just use the INSERT .. ON DUPLICATE KEY construct. Good luck, -- Willem Bogaerts Application smith Kratz B.V.http://www.kratz.nl/ No, I'm afraid not. :-( In this case, I'm actually working with classified ads, and it's a regular occurrence that one ad would fit into multiple sections. So when I copy the row, I'm checking the username, section name, and title of the ad, but none of these fields would technically be unique. The more I research, I don't think that there's a way to do it without a PHP script of some sort. |
#7
| ||||
| ||||
|
|
I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table SELECT NULL, NULL, var2, var3 FROM table WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL AND NOT EXISTS (SELECT var1 FROM table WHERE var1="something_else" ); Maybe you have to change this subquery to match your exact needs..... -- Luuk I think that comes pretty close, thanks, Luuk. One question, though; how do I make it SELECT based on dynamic variables from the first SELECT? I'm thinking the DEFAULT statement will work, but I don't know how to use it correctly. The docs on this were pretty vague. Something like: BEGIN DECLARE @var4; INSERT INTO table SELECT NULL, NULL, var2, var3 FROM table WHERE var2="something" SET @var4 = var3; UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL AND NOT EXISTS (SELECT var1 FROM table WHERE var1="something_else" AND var3=var4); END; Is this possible, or am I going to have to write a PHP script for this? |
|
colA | colB | +----------+----------+ 33996344 | 33996351 | 50331648 | 67276831 | 50331648 | 68257567 | 67276832 | 67276847 | 67276848 | 67277023 | 67277024 | 67277031 | +----------+----------+ |
|
colA | @b:=colB | +----------+----------+ 67276832 | 67276847 | +----------+----------+ |
|
@b | +----------+ 67276847 | +----------+ |
#8
| |||
| |||
|
|
On 9/12/2011 5:42 AM, jwcarlton wrote: On Sep 12, 3:44 am, Willem Bogaerts<w.bogae... (AT) kratz (DOT) nl> *wrote: On 10/09/11 23:16, jwcarlton wrote: I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table * *SELECT NULL, NULL, var2, var3 * *FROM table * *WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" ANDvar1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason Is there a unique index that would prevent the duplicates? If so, just use the INSERT .. ON DUPLICATE KEY construct. Good luck, -- Willem Bogaerts Application smith Kratz B.V.http://www.kratz.nl/ No, I'm afraid not. :-( *In this case, I'm actually working with classified ads, and it's a regular occurrence that one ad would fit into multiple sections. So when I copy the row, I'm checking the username, section name, and title of the ad, but none of these fields would technically be unique. The more I research, I don't think that there's a way to do it without a PHP script of some sort. Is the combination of the three unique? How would you determine duplicates in PHP? -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstuck... (AT) attglobal (DOT) net ================== |
#9
| |||
| |||
|
|
On 9/12/2011 5:42 AM, jwcarlton wrote: On Sep 12, 3:44 am, Willem Bogaerts<w.bogae... (AT) kratz (DOT) nl> *wrote: On 10/09/11 23:16, jwcarlton wrote: I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table * *SELECT NULL, NULL, var2, var3 * *FROM table * *WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" ANDvar1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason Is there a unique index that would prevent the duplicates? If so, just use the INSERT .. ON DUPLICATE KEY construct. Good luck, -- Willem Bogaerts Application smith Kratz B.V.http://www.kratz.nl/ No, I'm afraid not. :-( *In this case, I'm actually working with classified ads, and it's a regular occurrence that one ad would fit into multiple sections. So when I copy the row, I'm checking the username, section name, and title of the ad, but none of these fields would technically be unique. The more I research, I don't think that there's a way to do it without a PHP script of some sort. Is the combination of the three unique? How would you determine duplicates in PHP? -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstuck... (AT) attglobal (DOT) net ================== |
#10
| |||
| |||
|
|
On Sep 12, 8:48 am, Jerry Stuckle<jstuck... (AT) attglobal (DOT) net> wrote: On 9/12/2011 5:42 AM, jwcarlton wrote: On Sep 12, 3:44 am, Willem Bogaerts<w.bogae... (AT) kratz (DOT) nl> wrote: On 10/09/11 23:16, jwcarlton wrote: I'm copying one row into the same table, leaving one field empty. Then, I'm running a second line to fill that field if it's empty. Like so: INSERT INTO table SELECT NULL, NULL, var2, var3 FROM table WHERE var2="something"; UPDATE table SET var1="something_else" WHERE var2="something" AND var1 IS NULL; ("NULL, NULL" represents "id, var1", where "id" is an autoincrement) This will grab, for example, 100 rows and copy them for me, changing the one value that I designate. What I'm wanting to do is change the INSERT so that it only inserts IF there's not already a row in the table that has the same var1 that I've entered, and the same var2 and var3 as the one being selected. This will help me to prevent accidental duplicates. Any suggestions would be greatly appreciated! TIA, Jason Is there a unique index that would prevent the duplicates? If so, just use the INSERT .. ON DUPLICATE KEY construct. Good luck, -- Willem Bogaerts Application smith Kratz B.V.http://www.kratz.nl/ No, I'm afraid not. :-( In this case, I'm actually working with classified ads, and it's a regular occurrence that one ad would fit into multiple sections. So when I copy the row, I'm checking the username, section name, and title of the ad, but none of these fields would technically be unique. The more I research, I don't think that there's a way to do it without a PHP script of some sort. Is the combination of the three unique? How would you determine duplicates in PHP? -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstuck... (AT) attglobal (DOT) net ================== I was trying to not make it too confusing by giving a ton of field names. But I would compare the section, category, subcategory, username, title, and description. In plain English, the code would say "copy category, subcategory, username, title, and description, then change section to whatever is designated, UNLESS a row already exists with the new section, category, subcategory, username, title, and description". This wouldn't be limited to a single row, though. Someone could potentially have dozens of ads, and I may want all of them copied to another section (or several sections). The code that I posted in the first post works fine when I copy the rows to a second section. But, it's not uncommon that I copied some yesterday, and today the username created more ads; so, I want to only copy the new ones from today. Or, I might want to copy all of them from Section A to Section C, where yesterday I just copied them to Section B. In that case, the code in the first post would copy all from Section A AND Section B to Section C, which would give me 2 copies of each ad in Section C instead of the desired 1 copy. If I did it in PHP, I would probably have to select the row, store the columns as variables, then run the insert script. This isn't ran or tested, so there could be typos and errors, but I'm thinking something like: snip code |
![]() |
| Thread Tools | |
| Display Modes | |
| |