ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
OKi have 2 problems and one causes the other. SO i would like to fix the first part to keep the second one from having to be done.
I am inserting multiple records into a dbase, php and mysql, using a string. I need to know how to code this so if the values already exist that it DOES NOT enter them.
For example. The insert code looks like:
mysql_select_db($database_main, $main);
foreach ($HTTP_SESSION_VARS['program'] AS $value) {
$sql = "INSERT INTO publisher_advertisers (account_id, program_id) VALUES (".$HTTP_SESSION_VARS['user_id'].", $value)";
mysql_query ($sql) or die(mysql_error());
}
Right now i am getting dups and want to keep that from happening.
Also, is there a way to delete the dups and leave just one record remaining so i can fix it for now!
You can define an unique constraint to the account_id,program_id field-combination so duplicates are denied at the database level. Not sure about the exact syntax in mysql, but in postgres
it was something like:
Alternatively you can insert only rows not existing in the table.
Few syntaxes:
Code:
insert into foo (col1,col2) select 127,42 where (127,42) not in (select * from foo);
insert into foo (col1,col2) select 127,42 where not exists (select 1 from foo where (col1,col2)=(127,42));
Pick the one you figure out faster or more readable,YMMV.
You have an error in your SQL syntax near 'where (29,126) not in (select * from test)' at line 1
with this code:
Code:
mysql_select_db($database_main, $main);
foreach ($HTTP_SESSION_VARS['active_users'] AS $value) {
$sql = "insert into test (account_id,program_id) select ".$HTTP_SESSION_VARS['program_id'].",$value where (".$HTTP_SESSION_VARS['program_id'].",$value) not in (select * from test)";
mysql_query ($sql) or die(mysql_error());
}
It can be writen in MySQL by using the rewrite-rules, but the result is not beatiful.
Yoks! What a mess!
Code:
insert into test SELECT 29,126 from test as t1 LEFT JOIN test as t2 ON (t2.account_id=29 and t2.program_id=126) where t2.account_id IS NULL and t2.program_id IS NULL;
What this does, is that it takes a cross product with two tables leaving the correct output.
Say that we have table test containing values
(1,10), (2,20), (5,27) and we would like to add a valuepair (2,20) there.
Now the select statement in insert does the following:
It first creates a row containing values (2,20). (The 'from test as t1'-part is just fakeing this to look as join operation). Now it joins the result in hand to the table test (called as t2). What we have now is:
(fake1, fake2, t2.account_id, t2.program_id) with rows:
2,20, 1, 10
2,20, 2, 20
2,20, 5,27
The "ON" part in the join does that t2.account_id has to be 2 and t2.program_id has to be 20. now only line left here is
2,20, 2,20
The where part says that t2.account_id has to be null and t2.program_id has to be null. This is not true for this line, so it is left out. Result is empty set, so this (2,20) is not added to the table.
If we want to add an other entry, like (2,50), now we have:
(fake1, fake2, t2.account_id, t2.program_id)
2,50, 1, 10
2,50, 2, 20
2,50, 5,27
where the ON part does that t2.account_id has to be 2 and program_id has to be 50. This holds for none of the rows, so it looks that nothing is left. But since this is a "LEFT JOIN", it means that there has to be atleast one row from the "left table", so what is remaining now is a row
(2,50,NULL,NULL).
Now the last where condition holds (they are both null), so the selection returns those values, which are ultimately inserted into the table.
This is braindamaged and pathologically complex thought -pattern that has to be used here just to archive an insert statement not inserting duplicates. Some things in MySQL are not in good shape..
The two forms I used in the first post doesn't work untill in the version 4.1 (support for subqueries). The last form should work fine in 4.0.18 (maybe in earlier versions too).
Gaah.. I get the same error by installing 3.23.58 and trying those sentences.
Fun, isn't it?
Plan B: Use the unique constraint with ignore keyword:
Code:
mysql> select version();
+-----------+
| version() |
+-----------+
| 3.23.58 |
+-----------+
1 row in set (0.00 sec)
mysql> create table test(account_id integer,program_id integer,unique(account_id,program_id));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values(29,142);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(29,142);
ERROR 1062: Duplicate entry '29-142' for key 1
mysql> insert into test values(29,15);
Query OK, 1 row affected (0.01 sec)
mysql> insert ignore into test values(27,3);
Query OK, 1 row affected (0.00 sec)
mysql> insert ignore into test values(27,3);
Query OK, 0 rows affected (0.00 sec)
mysql> insert ignore into test values(27,3);
Query OK, 0 rows affected (0.00 sec)
mysql>
Plan C: Do the check in application level. That is, first sql-select matching rows from the table to see if it already exists and don't add it if it does.
Yeah really this is SOOO lame howhard it is to do this in mySQL, older versions.
Um how would i use the php to check for dups? The prob is the table is used by another ap so i can not change the structure of the existing data table.
Plan c seems the best route, so what would my code look like to check for dups before inserting?
At the idea level:
mysql_select_db($database_main, $main);
foreach ($HTTP_SESSION_VARS['program'] AS $value) {
$sql = "SELECT count(*) from publisher_advertisers where
account_id=$HTTP_SESSION_VARS['user_id'] and
program_id=$value";
mysql_query($sql) or die(mysql_error());
if ( the result == 0 ) {
$sql = "INSERT INTO publisher_advertisers (account_id, program_id) VALUES (".$HTTP_SESSION_VARS['user_id'].", $value)";
mysql_query ($sql) or die(mysql_error());
}
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.