I can't handle the update statement, based on several tables. I've already viewed tutorials and forums, customized my code, tried, tried... But still doesn't work.
Code:
UPDATE `system`
JOIN System AS Sys ON service.Servicename_ID = Sys.FK_Service_ID
JOIN Hardware ON System.System_ID = Hardware.FK_System_ID
JOIN Verantwortlichkeit ON System.System_ID = Verantwortlichkeit.FK_System_ID
JOIN Zertifikate ON System.System_ID = Zertifikate.FK_System_ID
JOIN hardware_servername ON hardware.Servername = hardware_servername.Servername_ID
JOIN hardware_domaene ON hardware_domaene.Domaene_ID = hardware.Domaene
JOIN hardware_betriebssystem ON hardware_betriebssystem.Betriebssystem_ID = hardware.Betriebssystem
JOIN hardware_standort ON hardware_standort.Standort_ID = hardware.Standort
JOIN verantwortlichkeit_personen AS AA ON AA.Verantwortlichkeit_Personen_ID = system.Hersteller
JOIN verantwortlichkeit_personen AS AB ON AB.Verantwortlichkeit_Personen_ID = system.Externer_Partner
JOIN system_verfuegbarkeitsstufe ON system_verfuegbarkeitsstufe.Verfuegbarkeitsstufe_ID = system.Verfuegbarkeitsstufe
JOIN system_systemname ON system_systemname.Systemname_ID = system.Systemname
JOIN system_systemnummer ON system_systemnummer.Systemnummer_ID = system.Systemnummer
JOIN verantwortlichkeit_rolle ON verantwortlichkeit_rolle.Verantwortlichkeit_Rolle_ID = verantwortlichkeit.Rolle
JOIN verantwortlichkeit_personen AS A0 ON A0.Verantwortlichkeit_Personen_ID = verantwortlichkeit.Hauptverantwortlicher
JOIN verantwortlichkeit_personen AS A1 ON A1.Verantwortlichkeit_Personen_ID = verantwortlichkeit.Stellvertretung
JOIN verantwortlichkeit_personen AS A2 ON A2.Verantwortlichkeit_Personen_ID = verantwortlichkeit.Wissenstraeger_1
JOIN verantwortlichkeit_personen AS A3 ON A3.Verantwortlichkeit_Personen_ID = verantwortlichkeit.Wissenstraeger_2
JOIN verantwortlichkeit_personen AS A4 ON A4.Verantwortlichkeit_Personen_ID = verantwortlichkeit.Verantwortlichkeit_extern
SET
Sys.Systemnummer = '1',
Sys.Systembeschreibung = 'testbeschreubuzng',
Sys.Hersteller = 'Okan Koc',
Sys.Externer_Partner = 'Okan Koc',
Sys.`BKAG_Brugg` = b'1',
Sys.`BCIAG_Brugg` = b'1',
Sys.`Cables_Diverse_PL_CN_US_DE` = b'1',
Sys.`BINF` = b'1',
Sys.`BRAG_Kleindoettingen` = b'1',
Sys.`BRG_Wunstorf` = b'1',
Sys.`BGP_Nordhausen` = b'1',
Sys.`Pipes_Diverse_FR_PL_IT_US` = b'1',
Sys.`Ropes_BDAG_Birr` = b'1',
Sys.`HOLDING_Brugg` = b'1',
Sys.`BIAG_Brugg` = b'1',
Sys.`BPK_Brugg` = b'1',
Sys.`Dritte_Schule_Brugg_LithCom_usw` = b'1',
Sys.`Verfuegbarkeitsstufe` = '1',
Sys.`Datenverlust` = '1',
Sys.`Systemname` = 'Testsystem',
hardware.`Servername` = 'srvntstest',
hardware.`Domaene` = 'cables.bruggnet.com',
hardware.`Betriebssystem` = 'Windows Server 2012 R2',
hardware.`Lokale_Datenbank` = '1',
hardware.`Datenbank_SQL_Master` = 'SQLTEST',
hardware.`Instanzname` = 'Testinstanz',
hardware.`Standort` = 'RZ 1',
hardware.`Backup_mit` = 'Veeam',
verantwortlichkeit.`Rolle` = 'Systemtechnik',
verantwortlichkeit.`Hauptverantwortlicher` = 'David Grunder',
verantwortlichkeit.`Stellvertretung` = 'Robin Gloor',
verantwortlichkeit.`Wissenstraeger_1` = 'Okan Koc',
verantwortlichkeit.`Wissenstraeger_2` = 'Marc Gerber',
verantwortlichkeit.`Verantwortlichkeit_extern` = 'infor',
zertifikate.`Zertifikatname` = 'testzertifikat',
zertifikate.`Ablaufdatum` = '27.12.2001'
WHERE Sys.System_ID = 1
It's impossible to spot all possible errors in such a long query but the second line contains two errors as I see it, Service is not defined and you're joining with the System table itself
UPDATE `system`
JOIN System AS Sys ON service.Servicename_ID = Sys.FK_Service_ID
It needs to be changed to something like
UPDATE System Sys
JOIN Service ON Service.Servicename_ID = Sys.FK_Service_ID
Related
I'm trying to update data in my table.
I have two tables, t1 and t2
T1
T2
I'm want to do, that if t1 have id_avito = null and all_usl_name = %usl_name1% and all_tel = %tel1%, and t2 have id != null, usl_name = %usl_name1% and tel = %tel1%
For example, t1 after execute query must to look like that
update people.t1, people.t2
set
id_avito = people.t2.id,
lnk_avito = people.t2.link,
all_price = people.t2.price,
all_date = people.t2.date,
all_adr = people.t2.adr,
all_usl_name = people.t2.usl_name
where id_avito != people.t2.id
and all_tel= people.t2.tel
and all_usl_type = people.t2.usl_type
I try to do like this, but it is not working
UPD
EXAMPLE: tables. Table before update, after update, and second table
Try with update join also you need to use like operator where you are searching with string
update people.t1 a
join people.t2 on id_avito != people.t2.id
and all_tel= people.t2.tel
and all_usl_type = people.t2.usl_type
set
id_avito = people.t2.id,
lnk_avito = people.t2.link,
all_price = people.t2.price,
all_date = people.t2.date,
all_adr = people.t2.adr,
all_usl_name = people.t2.usl_name
Please try this.
For SQL
UPDATE A
SET
A.id_avito = B.id,
A.lnk_avito = B.link,
A.all_price = B.price,
A.all_date = B.date,
A.all_adr = B.adr,
A.all_usl_name = B.usl_name
FROM
people.t1 A
INNER JOIN people.t2 B
ON A.id_avito != B.id
AND A.all_tel= B.tel
AND A.all_usl_type =B.usl_type
where ISNULL(A.id_avito,'') ='' and A.all_usl_name like '%usl_name1%' and A.all_tel like '%tel1%' and B.id is not null and B.usl_name like '%usl_name1%' and B.tel like '%tel1%'
FOR MYSQL
UPDATE people.t1 a
INNER JOIN people.t2 B
ON A.id_avito != B.id
AND A.all_tel= B.tel
AND A.all_usl_type =B.usl_type
SET
A.id_avito = B.id,
A.lnk_avito = B.link,
A.all_price = B.price,
A.all_date = B.date,
A.all_adr = B.adr,
A.all_usl_name = B.usl_name
where IFNULL(A.id_avito,'') = '' and A.all_usl_name like '%usl_name1%' and A.all_tel like '%tel1%' and IFNULL(B.id,0) <> 0 and B.usl_name like '%usl_name1%' and B.tel like '%tel1%'
I have this query and when I add a OR, return multiples rows, how I can fix this? I need another left join or Right join?
SELECT `images`.`id` as `id_image`, `images_lang`.`title` as `title`, `images_lang`.`autor` as `author`, `media_lang`.`file`, `category_lang`.`title` as `category`
FROM `images`
JOIN `images_lang` ON `images_lang`.`id_images` = `images`.`id`
JOIN `category_lang` ON `images`.`id_category` = `category_lang`.`id_category`
JOIN `media` ON `images`.`id` = `media`.`id_item`
JOIN `media_lang` ON `media`.`id` = `media_lang`.`id_media`
JOIN `relation` ON `relation`.`from_id` = `images`.`id`
JOIN `tag` ON `tag`.`id` = `relation`.`to_id`
JOIN `tag_lang` ON `tag`.`id` = `tag_lang`.`id_lang`
WHERE `media`.`table` = 'images'
AND `media_lang`.`id_lang` = '1'
AND `images_lang`.`id_lang` = '1'
AND `category_lang`.`id_lang` = '1'
AND `images`.`active` = 1
AND `relation`.`type` = 2
AND `tag_lang`.`id_lang` = '1'
AND `tag_lang`.`slug` = 'pa-am-oli'
OR `tag_lang`.`slug` = 'playa'
LIMIT 9
and the code in codeigniter, but I donĀ“t how add the or after the and ($keywords is an array for filter the elements)
$this->db->select('images.id as id_image, images_lang.title as title, images_lang.autor as author, media_lang.file, category_lang.title as category');
$this->db->from($this->table);
$this->db->join($this->table.'_lang',$this->table.'_lang.id_images = '.$this->table.'.id');
$this->db->join('category_lang',$this->table.'.id_category = category_lang.id_category');
$this->db->join('media',$this->table.'.id = media.id_item');
$this->db->join('media_lang','media.id = media_lang.id_media');
$this->db->where('media.table',$this->table);
$this->db->where('media_lang.id_lang',$id_lang);
$this->db->where($this->table.'_lang.id_lang', $id_lang);
$this->db->where('category_lang.id_lang', $id_lang);
$this->db->where('images.active', 1);
if($category){
$this->db->where('category_lang.title', $category);
}
if($keywords){
$this->db->join('relation', 'relation.from_id = '.$this->table.'.id');
$this->db->join('tag', 'tag.id = relation.to_id');
$this->db->join('tag_lang', 'tag.id = tag_lang.id_lang');
$this->db->where('relation.type', _IMAGES_2_TAGS_);
$this->db->where('tag_lang.id_lang', $id_lang);
foreach($keywords as $tag){
$this->db->or_where('tag_lang.slug', $tag);
}
}
if($from || $limit)
{
$this->db->limit((int)$limit, (int)$from);
}
$query = $this->db->get();
return $query->result_array();
I solved the query adding this (and a Select Distinct)
AND (`tag_lang`.`slug` = 'playa'
OR `tag_lang`.`slug` = 'pa-am-oli')
How I can add this in codeigniter?
I don't get what you need exactly, but if you want have just one line as a return by filtering using OR
In your case you should do the following:
WHERE (`media`.`table` = 'images' OR `tag_lang`.`slug` = 'playa')
AND (`media_lang`.`id_lang` = '1' OR `tag_lang`.`slug` = 'playa')
and so on, I hope that helps you and good luck!
Astute use of parentheses would solve your problem, but this is easier to read...
SELECT i.id id_image
, il.title
, il.autor author
, ml.file
, cl.title category
FROM images i
JOIN images_lang il
ON il.id_images = i.id
JOIN category_lang cl
ON cl.id_category = i.id_category
JOIN media m
ON m.id_item = i.id
JOIN media_lang ml
ON ml.id_media = m.id
JOIN relation r
ON r.from_id = i.id
JOIN tag t
ON t.id = r.to_id
JOIN tag_lang tl
ON tl.id_lang = t.id
WHERE m.table = 'images'
AND ml.id_lang = 1
AND il.id_lang = 1
AND cl.id_lang = 1
AND i.active = 1
AND r.type = 2
AND tl.id_lang = 1
AND tl.slug IN('pa-am-oli','playa')
LIMIT 9
... however, note that LIMIT without ORDER BY is fairly meaningless.
I have the next query
UPDATE (SELECT *
FROM bbdd_gestion.csvmagento as datos
INNER JOIN (SELECT va144.id as objetivo_id, va134.value as ean_final, va144.entity_id, va144.attribute_id, va144.value as objetivo_valor
FROM bbdd_magento.catalog_product_entity_varchar AS va134 INNER JOIN bbdd_magento.catalog_product_entity_varchar AS va144
ON va134.entity_id = va144.entity_id
WHERE va134.attribute_id = 134 AND va144.attribute_id = 144
AND va144.value = ''
) as final
WHERE datos.ean = final.ean_final) AS resultado
SET va144.value = resultado.marca
WHERE va144.value = '' AND resultado.ean_final = va134.value
But the result is
Error de SQL (1288): The target table resultado of the UPDATE is not updatable
How Solve this?
The solution is
UPDATE bbdd_magento.catalog_product_entity_varchar AS var144
INNER JOIN (SELECT *
FROM (SELECT *
FROM bbdd_gestion.csvmagento as datos
INNER JOIN (SELECT va144.id as objetivo_id, va134.value as ean_final, va144.entity_id, va144.attribute_id, va144.value as objetivo_valor
FROM bbdd_magento.catalog_product_entity_varchar AS va134 INNER JOIN bbdd_magento.catalog_product_entity_varchar AS va144
ON va134.entity_id = va144.entity_id
WHERE va134.attribute_id = 134 AND va144.attribute_id = 144
AND va144.value = ''
) as final
WHERE datos.ean = final.ean_final) AS resultado
WHERE resultado.ean_final = resultado.ean) as todo
SET var144.value = todo.marca
WHERE var144.entity_id = todo.entity_id
AND var144.attribute_id = 144
AND var144.value = ''
I have the following query and I would like to make it so if there's a duplicate key it updates the values
INSERT INTO totalData (pageId, dateScanned, totalPageLikes, totalTalkingAbout, totalPos, totalNeg, totalFemales, totalMales, totalStrongPositives, totalPositives, totalWeakPositives, totalNeutrals, totalWeakNegatives, totalNegatives, totalStrongNegatives, totalStatuses, totalStatusLikes, totalStatusShares, totalComments, totalUniqueCommenters)
SELECT pages.pageId, pages.dateScanned, pages.likes, pages.talkingAbout,
SUM(commentTags.tag LIKE '%positive%') AS positive,
SUM(commentTags.tag LIKE '%negative%') AS negative,
SUM(comments.gender = 'female') AS females,
SUM(comments.gender = 'male') AS males,
SUM(commentTags.tag = 'strong_positive') AS strongPositives,
SUM(commentTags.tag = 'positive') AS positives,
SUM(commentTags.tag = 'weak_positive') AS weakPositives,
SUM(commentTags.tag = 'neutral') AS neutrals,
SUM(commentTags.tag = 'weak_negative') AS weakNegatives,
SUM(commentTags.tag = 'negative') AS negatives,
SUM(commentTags.tag = 'strong_negative') AS strongNegatives,
COUNT(DISTINCT statuses.statusId) AS totalStatuses,
SUM(DISTINCT statuses.likesCount) AS totalLikesCount,
SUM(DISTINCT statuses.sharesCount) AS totalSharesCount,
COUNT(DISTINCT comments.commentId) AS totalComments,
COUNT(DISTINCT comments.userName) AS uniqueUsers
FROM pages
JOIN statuses ON pages.pageId = statuses.pageId AND pages.dateScanned = statuses.dateScanned
JOIN comments ON comments.statusID = statuses.statusId
JOIN commentTags ON comments.commentId = commentTags.commentId
WHERE pages.pageId = '115798033817' AND pages.dateScanned = '2013-11-05'
I tried the ON DUPLICATE KEY UPDATE and this is how I further modified the query
ON DUPLICATE KEY UPDATE
totalData.pageId = pageId, totalData.dateScanned = dateScanned,
totalData.totalPageLikes = totalPageLikes, totalData.totalTalkingAbout = totalTalkingAbout,
totalData.totalPos = positive, totalData.totalNeg = negative, totalData.totalFemales = females,
totalData.totalMales = males, totalData.totalStrongPositives = strongPositives,
totalData.totalPositives = positives, totalData.totalWeakPositives = weakPositives,
totalData.totalNeutrals = neutrals, totalData.totalWeakNegatives = weakNegatives,
totalData.totalNegatives = negatives, totalData.totalStrongNegatives = strongNegatives,
totalData.totalStatuses = totalStatuses, totalData.totalStatusLikes = totalLikesCount,
totalData.totalStatusShares = totalSharesCount, totalData.totalComments = totalComments,
totalData.totalUniqueCommenters = uniqueUsers ;
But when i run the query it says Unknown column 'positive' in field list.
Got it, I had to use the VALUES() function so the ON DUPLICATE KEY UPDATE part of the query becomes like the following
ON DUPLICATE KEY UPDATE
totalPageLikes = VALUES(totalPageLikes), totalTalkingAbout = VALUES(totalTalkingAbout),
totalPos = VALUES(totalPos), totalNeg = VALUES(totalNeg), totalFemales = VALUES(totalFemales), totalMales = VALUES(totalMales),
totalStrongPositives = VALUES(totalStrongPositives), totalPositives = VALUES(totalPositives), totalWeakPositives = VALUES(totalWeakPositives),
totalNeutrals = VALUES(totalNeutrals), totalWeakNegatives = VALUES(totalWeakNegatives), totalNegatives = VALUES(totalNegatives), totalStrongNegatives = VALUES(totalStrongNegatives),
totalStatuses = VALUES(totalStatuses), totalStatusLikes = VALUES(totalStatusLikes), totalStatusShares = VALUES(totalStatusShares),
totalComments = VALUES(totalComments), totalUniqueCommenters = VALUES(totalUniqueCommenters) ";
I wanted to run this query:
UPDATE up SET up.pts = uc.checkin_worth WHERE uc.email = up.email AND uc.company_id = up.company_id AND uc.email = 'test#gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'
However, I am getting an error because I don't know how to combine two tables (uc and up) in a UPDATE query.
Can anyone help me solve this?
Thanks,
Just use the normal JOIN syntax:
UPDATE up JOIN uc ON uc.email = up.email AND uc.company_id = up.company_id
SET up.pts = uc.checkin_worth
WHERE uc.email = 'test#gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'
You can also use the old comma syntax, which is more similar to your original query:
UPDATE uc, up
SET up.pts = uc.checkin_worth
WHERE uc.email = up.email
AND uc.company_id = up.company_id
AND uc.email = 'test#gmail.com'
AND uc.company_id = '4'
AND uc.qrcode = 'j'
Try with this:
UPDATE up, uc
SET up.pts = uc.checkin_worth,
WHERE uc.email = up.email AND uc.company_id = up.company_id AND uc.email = 'test#gmail.com' AND uc.company_id = '4' AND uc.qrcode = 'j'