1

メールアドレスが list_email.email_addr に存在せず、かつ list_no_email.email_addr にも存在しない場合、新しいレコードを挿入しようとしています

INSERT INTO list_email(fname, lname, email_addr) VALUES('bob', 'schmoe', 'bogus@bogus.com'), ('mary', 'lamb', 'hoe@me.com');
SELECT email_addr FROM list_email
WHERE NOT EXIST(
SELECT email_addr FROM email_addr WHERE email_addr = $post_addr
) 
WHERE NOT IN (
SELECT email_addr FROM list_no_email WHERE email_addr = $post_addr
)LIMIT 1


**************************** mysql tables ******************************

mysql> desc list_email;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| list_name  | varchar(55)  | YES  |     | NULL    |                |
| fname      | char(50)     | YES  |     | NULL    |                |
| lname      | char(50)     | YES  |     | NULL    |                |
| email_addr | varchar(150) | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

5 rows in set (0.00 sec)

mysql> desc list_no_email;
+------------+--------------+------+-----+-------------------+-----------------------------+
| Field      | Type         | Null | Key | Default           | Extra                       |
+------------+--------------+------+-----+-------------------+-----------------------------+
| id         | int(11)      | NO   | PRI | NULL              | auto_increment              |
| date_in    | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| email_addr | varchar(150) | YES  |     | NULL              |                             |
+------------+--------------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)

** * ** * ** * ** * ** * **エラー * ** * ** * ** * ** * *

INSERT INTO list_email(fname, lname, email_addr) VALUES('bob', 'schmoe', 'bogus@bogus.com'), ('mary', 'lamb', 'hoe@me.com'); SELECT email_addr FROM list_email WHERE NOT EXIST( SELECT email_addr FROM email_addr WHERE email_addr = $post_addr )  WHERE NOT IN ( SELECT email_addr FROM list_no_email WHERE email_addr = $post_addr )LIMIT 1;
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT email_addr FROM email_addr WHERE email_addr = $post_addr )  WHERE NOT IN ' at line 1

** * ** * ** * ** * * なし; * ** * ** * ** * ** * ***

mysql> INSERT INTO list_email(fname, lname, email_addr) VALUES('bob', 'schmoe', 'bogus@bogus.com'), ('mary', 'lamb', 'hoe@me.com')
    -> SELECT email_addr FROM list_email AS tmp
    -> WHERE NOT EXIST(
    -> SELECT email_addr FROM email_addr WHERE email_addr = $post_addr
    -> )
    -> WHERE NOT IN (
    -> SELECT email_addr FROM list_no_email WHERE email_addr = $post_addr
    -> )LIMIT 1;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT email_addr FROM list_email AS tmp
WHERE NOT EXIST(
SELECT email_addr FROM' at line 2
4

3 に答える 3

1

次のようなことを試してください:

 INSERT INTO list_email($username.$rowname, fname, lname, list_email) VALUES(?,?,?,?,?)
     SELECT email_addr FROM list_email AS tmp
     WHERE email_addr NOT IN(
         SELECT email_addr FROM list_email WHERE email_addr = $post_addr)
         WHERE email_addr NOT IN (SELECT email_addr FROM list_no_email 
                                  WHERE email_addr = $post_addr)
     LIMIT 1;
于 2013-09-03T05:28:13.490 に答える
0
INSERT INTO list_email
  (fname, lname, email_addr)
SELECT ins.*
FROM
  ( SELECT 'bob' AS fname, 
           'schmoe' AS lname, 
           'bogus@bogus.com' AS email_addr
    FROM dual
  UNION ALL                               -- for every row to be inserted
    SELECT 'mary', 'lamb', 'hoe@me.com'
    FROM dual 
                                          -- add another UNION ALL .. FROM dual
  ) AS ins
WHERE NOT EXISTS
      ( SELECT 1 
        FROM email_addr AS e 
        WHERE e.email_addr = ins.email_addr
      )
  AND NOT EXISTS
      ( SELECT 1 
        FROM list_no_email AS ne 
        WHERE ne.email_addr = ins.email_addr
      ) ;
于 2013-09-03T06:22:45.990 に答える
0

交換

WHERE NOT EXIST(

WHERE email_addr NOT IN(

編集

交換

SELECT email_addr FROM list_email
WHERE NOT IN
(
    SELECT email_addr FROM email_addr WHERE email_addr = $post_addr
) 
WHERE NOT IN
(
    SELECT email_addr FROM list_no_email WHERE email_addr = $post_addr
)LIMIT 1

SELECT email_addr FROM list_email
WHERE NOT IN
(
    SELECT email_addr FROM email_addr WHERE email_addr = $post_addr
    UNION ALL
    SELECT email_addr FROM list_no_email WHERE email_addr = $post_addr
)
LIMIT 1
于 2013-09-03T05:15:55.373 に答える