I have no idea how to do what i'm trying to do.
Hopefully the data relationships are demonstrated in this example
UPDATE sym_entries_data_55'
SET value = '46.00'
WHERE (sym_entries_data_55.id = sym_entries_data_54.id)
AND (sym_entries_data_54.member_id = sym_entries_data_5.entry_id)
AND (sym_entries_data_5.username = 'namehere')
Answer
UPDATE sym_entries_data_55, sym_entries_data_54, sym_entries_data_5
SET sym_entries_data_55.value = '52.00'
WHERE sym_entries_data_55.id = sym_entries_data_54.id
AND sym_entries_data_54.member_id = sym_entries_data_5.entry_id
AND sym_entries_data_5.username = 'namehere'
UPDATE sym_entries_data_55, sym_entries_data_54, sym_entries_data_5
SET sym_entries_data_55.value = '46.00'
WHERE sym_entries_data_55.id = sym_entries_data_54.id
AND sym_entries_data_54.member_id = sym_entries_data_5.entry_id
AND sym_entries_data_5.username = 'namehere'
Related
I have two tables, Table A and Table B. Table A has 10k records. Table B has those 10K old records and additional records. I need to update the old 10k records in Table B.
I used,
UPDATE CONTENT_WORKFLOW_MASTER CWM, CWSCOPY TMP SET
CWM.PROGRAM_ID = TMP.PROGRAM_ID,
CWM.PROGRAM_TYPE_ID = TMP.PROGRAM_TYPE_ID,
CWM.PROGRAM_TYPE_NAME = TMP.PROGRAM_TYPE_NAME,
CWM.ORIGINAL_TITLE = TMP.ORIGINAL_TITLE,
CWM.SOURCE_GROUP_ID = TMP.SOURCE_GROUP_ID,
CWM.MASTER_TITLE = TMP.MASTER_TITLE,
CWM.PROGRAM_LANGUAGE = TMP.PROGRAM_LANGUAGE,
CWM.REGION = TMP.REGION,
CWM.CW_STATUS = 'NEW',
CWM.COPY_CULTURE = TMP.COPY_CULTURE,
CWM.GENRES = TMP.GENRES,
CWM.AIR_DATE_TIME = TMP.AIR_DATE_TIME,
CWM.PROCESS_ID = TMP.GUID,
CWM.PRIORITY = 0,
CWM.UPDATED_BY_SCHEDULE = 1,
CWM.MODIFIED_USER = 'StoredProcUser',
CWM.MODIFIED_DATE = NOW() WHERE CWM.PROGRAM_ID = TMP.PROGRAM_ID ;
And i used,
UPDATE CONTENT_WORKFLOW_MASTER CWM INNER JOIN t.temp TMP
USING (PROGRAM_ID)
SET
CWM.PROGRAM_ID = TMP.PROGRAM_ID,
CWM.PROGRAM_TYPE_ID = TMP.PROGRAM_TYPE_ID,
CWM.PROGRAM_TYPE_NAME = TMP.PROGRAM_TYPE_NAME,
CWM.ORIGINAL_TITLE = TMP.ORIGINAL_TITLE,
CWM.SOURCE_GROUP_ID = TMP.SOURCE_GROUP_ID,
CWM.MASTER_TITLE = TMP.MASTER_TITLE,
CWM.PROGRAM_LANGUAGE = TMP.PROGRAM_LANGUAGE,
CWM.REGION = TMP.REGION,
CWM.CW_STATUS = 'NEW',
CWM.COPY_CULTURE = TMP.COPY_CULTURE,
CWM.GENRES = TMP.GENRES,
CWM.AIR_DATE_TIME = TMP.AIR_DATE_TIME,
CWM.PROCESS_ID = TMP.GUID,
CWM.PRIORITY = 0,
CWM.UPDATED_BY_SCHEDULE = 1,
CWM.MODIFIED_USER = 'StoredProcUser',
CWM.MODIFIED_DATE = NOW() WHERE CWM.USER_LOCKED=0;
create TEMPORARY table t.temp AS
SELECT DISTINCT CWS.CONTENT_WORKFLOW_STAGING_ID,CWS.PROGRAM_ID,CWS.SOURCE_GROUP_NAME,CWS.COPY_CULTURE,
CWS.PROGRAM_TYPE_ID, CWS.PROGRAM_TYPE_NAME, CWS.ORIGINAL_TITLE, CWS.SOURCE_GROUP_ID, CWS.MASTER_TITLE,
CWS.PROGRAM_LANGUAGE, CWS.REGION, CWS.GENRES, CWS.AIR_DATE_TIME, CWS.GUID,CWS.IS_PROCESSED, CWS.INTERNAL_TRANSACTION_ID
FROM CONTENT_WORKFLOW_STAGING CWS INNER JOIN CONTENT_WORKFLOW_MASTER CWM ON
CWM.PROGRAM_ID = CWS.PROGRAM_ID AND
CWM.SOURCE_GROUP_NAME = CWS.SOURCE_GROUP_NAME AND CWM.COPY_CULTURE = CWS.COPY_CULTURE AND CWM.USER_LOCKED = 0
AND
CWS.IS_PROCESSED = 0 AND CWS.INTERNAL_TRANSACTION_ID = INTERNAL_TRANSACTION_ID;
Both the queries are not full efficient. CAn anyone suggest the efficient way to to that. Within atleast 10 sec.
After uploading a csv file, I am trying to insert its contents into my database table. I have this query:
$connect = mysql_connect("localhost","root","");
mysql_select_db("dbtest",$connect);
//get the file
$handle = fopen($filename,"r");
do {
if (isset($data[0])) {
$data0 = mysql_real_escape_string($data[0]); //rcode
$data1 = mysql_real_escape_string($data[1]); //pcode
$data2 = mysql_real_escape_string($data[2]); //mcode
$data3 = mysql_real_escape_string($data[3]); //bcode
$data4 = mysql_real_escape_string($data[4]); //ecode
$data5 = mysql_real_escape_string($data[5]); //filetype
$data6 = mysql_real_escape_string($data[6]); //rec_count
$data7 = mysql_real_escape_string($data[7]); //gen_count
$data8 = mysql_real_escape_string($data[8]); //qc_count
$data9 = mysql_real_escape_string($data[9]); //be_count
$data10 = mysql_real_escape_string($data[10]); //trn_count
$query = "INSERT INTO tbltest(rcode,pcode,mcode,bcode,ecode,filetype,rec_count,
gen_count,qc_count,be_count,trn_count) VALUES ('$data0','$data1','$data2',
'$data3', '$data4', '$data5', '$data6', '$data7', '$data8', '$data9', '$data10')
ON DUPLICATE KEY UPDATE rec_count=values(rec_count),gen_count=values(gen_count),
qc_count=values(qc_count), be_count=values(be_count), trn_count=values(trn_count)";
mysql_query ($query,$connect) ;
}
} while ($data = fgetcsv($handle,1000,"|"));
And it's working neatly but then as the database was re-structured, I just then need to update the database table as rcode to filetype has values already and I just need to insert values of rec_count to trn_count. So my first query INSERT INTO ... ON DUPLICATE KEY UPDATE has been change to UPDATE only. And so I did this:
$query = "UPDATE tbltest SET (rec_count='$data6', gen_count = '$data7',
qc_count = '$data8', be_count = '$data9', trn_count= '$data10') WHERE
(rcode = '$data0', pcode = '$data1', mcode = '$data2', bcode = '$data3',
ecode = '$data4', filetype = '$data5')";
My problem now is that, my UPDATE seems to be not working as it doesn't update the database table. Bu when I did this;
$query = "UPDATE tbltest SET rcode = '5'";
The database is being updated. When I tried echo $query;, the echo responds the correct data (from the csv). I just cannot figure why it doesn't insert these data into the database. Kindly help. Thanks
Your SQL syntax is incorrect. The statement should read something like
UPDATE tbltest
SET rec_count='...', gen_count = '...', ...
WHERE rcode = '...' AND pcode = '...' AND ...
See MySQL UPDATE syntax.
I am getting the following error
"Lookup Error - MySQL Database Error: You can't specify target table
'customer_copy' for update in FROM clause"
while executing the following query:
Delete from customer_copy where code IN
(select cuc.code from customer_copy cuc
INNER JOIN customer_copy1 cu ON cuc.code = cu.code
where cuc.district=cu.district and cuc.city = cu.city and
cuc.name = cu.name and cuc.country = cu.country and
cuc.customer_grp1 = cu.customer_grp1 and
cuc.email = cu.email and cuc.fax = cu.fax and
cuc.house_number = cu.house_number and
cuc.name=cu.name and cuc.postal_code = cu.postal_code and
cuc.region = cu.region and cuc.street = cu.street and
cuc.tax_juris_code = cu.tax_juris_code and
cuc.tax_number1 = cu.tax_number1 and
cuc.tele = cu.tele and cuc.plant_code = cu.plant_code and
cuc.employee_code = cu.employee_code and
cuc.sales_district_code = cu.sales_district_code and
cuc.sales_office_code = cu.sales_office_code and
cuc.tax_number2 = cu.tax_number2 and
cuc.tax_number3 = cu.tax_number3 and
cuc.search_terms_1 = cu.search_terms_1 and
cuc.search_terms_2 = cu.search_terms_2);
in MYSQl database
The select query is working fine. What can possibly be wrong?
Instead of Delete from tableName just write Delete tableName
I have solve this problem. I had to use an alias name for the inner query. Don't know why but I have made the query run!!
here is my updated query.
delete cc.* from customer_copy cc where cc.code IN
(select code from (select cu.code from customer_copy cu
INNER JOIN customer_copy1 cuc ON cuc.code = cu.code
where cuc.district=cu.district and cuc.city = cu.city and
cuc.name = cu.name and cuc.country = cu.country and
cuc.customer_grp1 = cu.customer_grp1 and
cuc.email = cu.email and cuc.fax = cu.fax and
cuc.house_number = cu.house_number and
cuc.name=cu.name and cuc.postal_code = cu.postal_code and
cuc.region = cu.region and cuc.street = cu.street and
cuc.tax_juris_code = cu.tax_juris_code and
cuc.tax_number1 = cu.tax_number1 and
cuc.tele = cu.tele and cuc.plant_code = cu.plant_code and
cuc.employee_code = cu.employee_code and
cuc.sales_district_code = cu.sales_district_code and
cuc.sales_office_code = cu.sales_office_code and
cuc.tax_number2 = cu.tax_number2 and
cuc.tax_number3 = cu.tax_number3 and
cuc.search_terms_1 = cu.search_terms_1 and
cuc.search_terms_2 = cu.search_terms_2)x);
so my problem is this, I have a 3 part form that is a service ticket, the 1st part of the form is filled out by the dispatcher and submitted to mysql, the technician then see's they have a ticket assigned to them via email and they pull up the search result for tickets assigned to them and they click on a link in the search result that displays the 2nd form for them to enter what they did and what material and labor there is, this 2nd part of the form is my issue it has multiple values that are similar such as item_qty1, item_qty2, item_qty3 and so on. When I use more than 1 value like item_qty1 in my UPDATE tickets SET query I get a syntax error. Also, I am well aware that my code is subject to sql injection and I will deal with that when I have a working form. So here is my code:
<?php
// database connection //
include 'db_connect.php';
include 'data/var/variables.php';
//Writes the information to the database
mysql_query("UPDATE tickets SET work_performed = $work_performed,
item_qty1 = $item_qty1,
item_qty2 = $item_qty2,
item_qty3 = $item_qty3,
item_qty4 = $item_qty4,
item_qty5 = $item_qty5,
manuf_1 = $manuf_1,
manuf_2 = $manuf_2,
manuf_3 = $manuf_3,
manuf_4 = $manuf_4,
manuf_5 = $manuf_5,
part_number1 = $part_number1,
part_number2 = $part_number2,
part_number3 = $part_number3,
part_number4 = $part_number4,
part_number5 = $part_number5,
part_description1 = $part_description1,
part_description2 = $part_description2,
part_description3 = $part_description3,
part_description4 = $part_description4,
part_description5 = $part_description5,
part_price1 = $part_price1,
part_price2 = $part_price2,
part_price3 = $part_price3,
part_price4 = $part_price4,
part_price5 = $part_price5,
price_extension1 = $price_extension1,
price_extension2 = $price_extension2,
price_extension3 = $price_extension3,
price_extension4 = $price_extension4,
price_extension5 = $price_extension5,
material_total = $material_total,
sales_tax = $sales_tax,
shipping_cost = $shipping_cost,
work_date1 = $work_date1,
work_date2 = $work_date2,
work_date3 = $work_date3,
work_date4 = $work_date4,
work_date5 = $work_date5,
tech_name1 = $tech_name1,
tech_name2 = $tech_name2,
tech_name3 = $tech_name3,
tech_name4 = $tech_name4,
tech_name5 = $tech_name5,
cost_code1 = $cost_code1,
cost_code2 = $cost_code2,
cost_code3 = $cost_code3,
cost_code4 = $cost_code4,
cost_code5 = $cost_code5,
pay_rate1 = $pay_rate1,
pay_rate2 = $pay_rate2,
pay_rate3 = $pay_rate3,
pay_rate4 = $pay_rate4,
pay_rate5 = $pay_rate5,
total_hours1 = $total_hours1,
total_hours2 = $total_hours2,
total_hours3 = $total_hours3,
total_hours4 = $total_hours4,
total_hours5 = $total_hours5,
hours_subtotal1 = $hours_subtotal1,
hours_subtotal2 = $hours_subtotal2,
hours_subtotal3 = $hours_subtotal3,
hours_subtotal4 = $hours_subtotal4,
hours_subtotal5 = $hours_subtotal5,
total_hours = $total_hours,
material_total = $material_total,
labor_cost = $labor_cost,
grand_total = $grand_total WHERE `id` = '$id'");
mysql_affected_rows();
echo mysql_error();
?>
The code as it is wont post to the database, it displays the error:
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 ' item_qty1 = , item_qty2 = , item_qty3 = ' at line 1
I was told I don't need apostrophe's or back ticks on my values so I removed them and still get the error. Now I changed my code a bit to remove any similar values, example I removed any value beyond value 1 so item_qty2, item_qty3 and so on I removed so I now have this code:
mysql_query("UPDATE `tickets` SET `work_performed` = '$work_performed',
`item_qty1` = '$item_qty1',
`manuf_1` = '$manuf_1',
`part_number1` = '$part_number1',
`part_description1` = '$part_description1',
`part_price1` = '$part_price1',
`price_extension1` = '$price_extension1',
`material_total` = '$material_total',
`sales_tax` = '$sales_tax',
`shipping_cost` = '$shipping_cost',
`work_date1` = '$work_date1',
`tech_name1` = '$tech_name1',
`cost_code1` = '$cost_code1',
`pay_rate1` = '$pay_rate1',
`total_hours1` = '$total_hours1',
`hours_subtotal1` = '$hours_subtotal1',
`total_hours` = '$total_hours',
`material_total` = '$material_total',
`labor_cost` = '$labor_cost',
`grand_total` = '$grand_total' WHERE `id` = '$id'");
This modified code works flawlessly everytime with no syntax errors and posts to the selected record every single time, but, this wont work for me, I need the additional values OR I need a way for the user to add additional fields to the form if they need to which would solve my problem of not having to enter a value in every field unless they have to. Also if anybody has any examples on how to compress this to make it more "functional" not so bulky I guess, that would be appreciated very much. Thanks!
to just optimize the code, what can be done is :
// define an array of column names and values got from input.
$column_names = array('column1' => $column1, 'column2' => $column2, .....);
// built an sql select clause
$select_clause = array();
foreach ($column_names as $cn => $cn_val) {
if (!empty($cn_val)) {
$select_clause = "{$cn} = {$cn_val}";
}
}
// built proper query
$sql = "UPDATE table_name SET" . implode(',', $select_clause) . " table_name WHERE .....";
// continue with your stuff.
Is it possible to merge these queries?
$sql1 = "UPDATE users AS u, invites AS i
SET u.uinvite = u.uinvite - 1, u.ufriends = CONCAT(u.ufriends, ',$iid'), i.seen = 1
WHERE u.uid = '$uid' AND i.invited_uid = '$uid' AND i.invitor_uid = '$iid'";
$sql2 = "UPDATE users
SET ufriends = CONCAT(ufriends, ',$uid')
WHERE uid = '$iid'";
Variable explanation:
$uid = the users ID $iid = the friends ID
This was my attempt to merge:
$mergesql = "UPDATE invites AS i, users AS u
SET
CASE
WHEN u.uid = '$uid' THEN u.uinvite=u.uinvite-1, u.ufriends = CONCAT(u.ufriends,',$iid')
ELSE u.ufriends = CONCAT(u.ufriends,',$uid')
END
, i.seen = '1'
WHERE i.invited_uid = '$uid' AND i.invitor_uid = '$iid'
AND (u.uid = '$uid' OR u.uid = '$iid')"
I find it hard to understand how mysql works when you have to do more complicated queries.
My English also lacks expertise, if you know a good tutorial, you can make me really happy with it! (not the one from the MySQL website, it's killing =S)
Knowing MySQL, there is probably a way to do this in one query, but I don't think you would get any benefit at all because you are updating based on totally separate keys. It might actually be dangerous to do this because the result would be unpredictable. Doing it in two queries, it's easy to figure out what's going on.
If it's not too late, I would suggest that you remove the friends field in favor of a mapping table between users and friends.
Did you try this:
$mergesql = "UPDATE
users AS u,
invites AS i,
users AS uf
SET
u.uinvite = u.uinvite - 1, u.ufriends = CONCAT(u.ufriends, ',$iid'),
i.seen = 1,
uf.ufriends = CONCAT(uf.ufriends, ',$uid')
WHERE u.uid = '$uid'
AND i.invited_uid = '$uid' AND i.invitor_uid = '$iid'
AND uid = '$iid'";
?