laravel export select with insert and update without duplicate - mysql

i got this query, i created a table with the fields matricula and kmsTotais. but when i try using a insert and then a update at the end it duplicate rows in the table, how can i make it so that it only updates the rows in the table?
$summaryData = DB::select('SELECT f.matricula, kmsTotais
FROM (
SELECT matricula, SUM(kmfim - quilometragem) AS kmsTotais
FROM formulario
GROUP BY formulario.matricula
) f
I'm trying to make an export of a report, i got raw sql query but i need to insert the query result to a table and update it , making it impossible to duplicate the report result of the query.
but i'm not being able to insert and update in the sql query.

Related

insert into select on duplicate mysql query

I am trying to update on duplicate record in MySQL,
I have a table with many column but i want to update only some column from another table with same desc as current table but it is not updating records.
my query is:
insert into backup_prochart.symbol_list(ticker,end_date,cur_date)
select ticker,t.end_date,t.cur_date from prochart.symbol_list t where
ticker=t.ticker and ticker= 'MAY17' on duplicate key update
end_date=t.end_date,cur_date=t.cur_date;
another query i tried
insert into backup_prochart.symbol_list(ticker,end_date,cur_date) select t.ticker,t.end_date,t.cur_date from prochart.symbol_list t where ticker=t.ticker and t.ticker= 'MAY17' on duplicate key update end_date=t.end_date,cur_date=t.cur_date;
can anyone tell me whats wrong with my query.?
You could try :
INSERT INTO backup_prochart.symbol_list (ticker, end_date, cur_date)
SELECT ticker, end_date, cur_date FROM prochart.symbol_list WHERE ticker = 'MAY17'
ON DUPLICATE KEY UPDATE end_date = values(end_date), cur_date = values(cur_date);
Of course the column "ticker" must be defined as unique for the table "backup_prochart.symbol_list".
You say that you are trying to update a record, but you are using an INSERT statement. Shouldn't you be using UPDATE instead of INSERT?
Difference between INSERT and UPDATE can be found here
Note that you can use UPDATE and SELECT in a single query.
try this. its worked for me.
INSERT INTO employee_projects
(employee_id,
proj_ref_code)
SELECT ep.employee_id,
ep.proj_ref_code
FROM hs_hr_emp_projects_history ep
WHERE NOT EXISTS (SELECT 1
FROM employee_projects p
WHERE ep.employee_id = p.employee_id
AND ep.proj_ref_code = p.proj_ref_code)

Mysql Insert if the Specific Column is not available for the same Employee

I have a controller to save a record
My Table contains BELOW Fields
This is Must (It has to repeat in LOOP).
I want to insert a record single time in a table for a Employee Id, and it should not repeat again. But Same Employee can have multiple Batch Ids and Multiple Course ID's.
If I take Unique as a Employee Id that is not working again to insert the another record to the same employee.
This process should repeat inside the loop and I need to get the Last Inserted ID from the Table and have to assign the number of students in the another table. This everything is working fine if I create a Procedure in Mysql and If I call Procedure. But my Linux server is not executing and throwing MySQL error.
Here is my query and
<code>
$insert_staff_assign = "insert into staff_assign
(`main_course_id`, `main_batch_id`, `section`, `semester_course_id`, `emp_mas_staff_id`, `emp_category`)
VALUES
(:main_course_id, :main_batch_id, :section_id, :semester_course_id, :emp_mas_staff_id, :emp_category)
ON DUPLICATE KEY UPDATE
main_course_id=:main_course_id, main_batch_id=:main_batch_id, section=:section_id, semester_course_id=:semester_course_id, emp_mas_staff_id=:emp_mas_staff_id, emp_category=:emp_category ";
insert into staff_assign
(`main_course_id`, `main_batch_id`, `section`, `semester_course_id`, `emp_mas_staff_id`, `emp_category`)
VALUES
(:main_course_id, :main_batch_id, :section_id, :semester_course_id, :emp_mas_staff_id, :emp_category)
ON DUPLICATE KEY UPDATE
main_course_id=:main_course_id, main_batch_id=:main_batch_id, section=:section_id, semester_course_id=:semester_course_id, emp_mas_staff_id=:emp_mas_staff_id, emp_category=:emp_category
insert into staff_assign
(`main_course_id`, `main_batch_id`, `section`, `semester_course_id`, `emp_mas_staff_id`, `emp_category`)
SELECT * FROM (
SELECT
:main_course_id, :main_batch_id, :section_id, :semester_course_id, :emp_mas_staff_id, :emp_category
) AS tmp WHERE NOT IN (
SELECT emp_mas_staff_id FROM staff_assign WHERE emp_mas_staff_id = $save_emp_mas_staff_id
) LIMIT 1
</code>
Please send me the query to get rid of this problem.
The above are my queries.
I found the answer for the above question.
mysql.proc problem.
In my mysql my mysql.proc was corrupted. That is the reason it doesn't execute the above query.
To fix the above issue you need to update mysql in linux.

insert statement sql inserting values into a table in mysql with a where clause condition

I have rails application and its using mysql.
I have a piles table with two columns that I care for.
The columns are name_he_il and name_en_us
I don't have a problem doing these
select name_he_il from piles;
select name_en_us from piles;
I need to insert data into the name_he_il column into the piles table where name_en_us = "a specific value"
I tried something like this
insert into piles (name_he_il) values 'לא מאפיין כלל' where name_en_us = "Extremely Uncharacteristic";
I am getting syntax error.
I was googling and I figured the sql should be
insert into table (column 1) values (blah) where conditions;
but its not working.
Basically that hebrew text means extremely uncharacteristic.
You want to use UPDATE ... WHERE
INSERT is for creating new records only.
Do UPDATE and not INSERT:
UPDATE piles SET name_he_il = 'לא מאפיין כלל' WHERE name_en_us = "Extremely Uncharacteristic";
Insert query is to insert new rom into table. If you already have row with value of "name_he_il" column then you need to use update.
UPDATE piles SET name_he_il='new value' WHERE name_he_es = 'something'

SQL grammar for INSERT SELECT ON DUPLICATE UPDATE

I want to GROUP and aggregate the rows from one table and insert into the other, and update the aggregate value. What is the SQL command for that?
INSERT INTO tbdetail (detail, views)
(SELECT detail, SUM(views) AS viewsall
FROM tb
GROUP BY detail)
//ON DUPLICATE KEY UPDATE views = views + viewsall <- how to update here?
In MySQL, when using an INSERT ... SELECT statement, you can refer to one of the values of the SELECT statement using the VALUES() function:
INSERT INTO tbdetail (detail, views)
SELECT detail, SUM(views) AS viewsall FROM tb GROUP BY detail
ON DUPLICATE KEY UPDATE views = views + VALUES(views);
More information here: http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_values

copy one column from one table to another

I am confused about how to copy a column from one table to another table using where. I wrote SQL query but it says transaction lock time exceeded or query returns more than one row.
using mysql
Basically,
I have:
Table 1: Results
BuildID platform_to_insert
Table 2: build
BuildID correct_platform
update results set results.platform_to_insert
= (select correct_platform from
build where results.BuildID = build.BuildID)
I do not believe you need a sub query.
UPDATE results, build
SET results.platform_to_insert = build.correct_platform
WHERE results.BuildID = build.BuildID
There are two options here:
update your tables to use BuildID as a primary key (to avoid duplicates)
update your subquery to only return one result
UPDATE results SET results.platform_to_insert = (
SELECT correct_platform
FROM build
WHERE results.BuildID=build.BuildID LIMIT 1
);