1

I have a table with 4 fields: field1, field2, field3, field4 and I want to insert registers that not exists if (field1 and field2 and field3) exists.

My query is this :

INSERT INTO mytable (`id_est`, `id_course`, `date`,`nro`)
VALUES ('5','7','2020-06-11','')
where id_est and id_course
  and date not in (select id_est, id_course, date from mytable)

I want two things:

  1. Know Why with this query appears the next error:

    #1064 - 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 'WHERE `id_est` and `id_course` and `DATE` not in (select `id_estudiante`,' at line 1

  2. How can I transform this query according to the CodeIgniter syntax?

    $data =array(
    )
    $this->db->insert('mytable', $data);
    $this->db->where_is_not();
    

Thanks. The field id_est and id_course and date are key and I wan't that is repeated.

Clarification: I need to insert data in mytable with the next fields: id_est, id_course, date and nro. I need that id_est and id_course and date (all these three fields are unique), for example:

id_est         id_course          date           nro
1                  1             date1            1
1                  1             date2            3
1                  1             date2            2 (this register is not insert because id_est=1 and id_course=1 and date2 is in my table)
Andriy M
  • 76,112
  • 17
  • 94
  • 154
cabita
  • 832
  • 2
  • 12
  • 36
  • for your syntex error remove the single quote(') from the fourth field nro in values from query. – Punit Nov 29 '11 at 11:37
  • this link will be helpful for you: [http://stackoverflow.com/questions/6047149/subquery-in-codeigniter-active-record][1] [1]: http://stackoverflow.com/questions/6047149/subquery-in-codeigniter-active-record – Punit Nov 29 '11 at 11:52

3 Answers3

0

1) Remove ''s around id_est, id_course, date and nro.

2) $data = array('id_est'=>'5','id_course'=>'7','date'=>'2020-06-11','nro'=>'');

Moyshe
  • 1,122
  • 1
  • 11
  • 19
  • Thanks for your answer. 1) Now appears this error: 1064 - 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 'WHERE id_est and id_course and DATE not in (select id_estudiante,' at line 1.............. 2) I want to know how to put that (id_est, and id_course, and date) not in .. in syntaxis codeigniter – cabita Nov 29 '11 at 11:38
  • 1) Well, did you check the manual? :) This `WHERE ...` is not present in your question, so I don't really know what are you asking me for. 2) Can you ask the question in different way? I can't understand. – Moyshe Nov 29 '11 at 11:41
  • Hi, I'm sorry, I have updated my question after to change the ''s. I'm sorry my english. Thanks for your help. – cabita Nov 29 '11 at 11:47
0

Try this:

INSERT INTO mytable (`id_est`, `id_course`, `date`, `nro`)
SELECT '5','7','2020-06-11',''
WHERE NOT EXISTS (SELECT * FROM mytable
where id_est = '5'
and id_course = '7'
and date = '2020-06-11')

I don't know code igniter, but you should be able to convert it once you get the query working

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • YOur idea is good but appears the next error: #1064 - 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 'WHERE NOT EXISTS (SELECT * FROM mytable where id_est =5 – cabita Nov 29 '11 at 12:00
0
INSERT INTO mytable (`id_est`, `id_course`, `date`, `nro`)
select '5','7','2020-06-11',"" 
from dual 
where NOT EXISTs (select nro from estudiante_curso_fallas where id_est =5 and id_course = 7 and date = '2020-06-11')

The error #1064 - 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 'WHERE NOT EXISTS (SELECT * FROM mytable where id_est =5

is because I am not using from dual. It's necessary from dual because the query and subquery are in the same table.

Thanks to all for your help. I'll search how put this query in syntax of codeigniter.

cabita
  • 832
  • 2
  • 12
  • 36