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) ";
Related
enter image description here
script :
SELECT
tb_biodata.nama_lengkap as nama,
tb_biodata.jenis_pegawai,
tb_biodata.cabang,
tb_penilaian.tanggal,
case when tb_user.grup = 'satu' and tb_user.jabatan = 'kepegawaian' then rata_rata end AS penilai1,
case when tb_user.grup = 'satu' and tb_user.jabatan = 'mg' then rata_rata end AS penilai2,
case when tb_user.cabang = 'Majapahit 605' and tb_user.jabatan = 'amg' then rata_rata end AS penilai3
FROM tb_biodata
inner join tb_penilaian ON tb_biodata.kode_biodata = tb_penilaian.kode_biodata
inner join tb_user ON tb_penilaian.kode_user = tb_user.kode_user
where tb_biodata.jenis_pegawai = 'pegawai kependidikan'
how to make the data into one line how
You can use GROUP BY and for the case use GROUP_CONCAT.
Assuming nama_lengkap is unique and then query will be as below.
If there will be duplicate try using primary Key in GROUP BY
SELECT
tb_biodata.nama_lengkap AS nama,
tb_biodata.jenis_pegawai,
tb_biodata.cabang,
tb_penilaian.tanggal,
GROUP_CONCAT(CASE WHEN tb_user.grup = 'satu' AND tb_user.jabatan = 'kepegawaian' THEN rata_rata END) AS penilai1,
GROUP_CONCAT(CASE WHEN tb_user.grup = 'satu' AND tb_user.jabatan = 'mg' THEN rata_rata END) AS penilai2,
GROUPCONCAT(CASE WHEN tb_user.cabang = 'Majapahit 605' AND tb_user.jabatan = 'amg' THEN rata_rata END) AS penilai3
FROM tb_biodata
INNER JOIN tb_penilaian ON tb_biodata.kode_biodata = tb_penilaian.kode_biodata
INNER JOIN tb_user ON tb_penilaian.kode_user = tb_user.kode_user
WHERE tb_biodata.jenis_pegawai = 'pegawai kependidikan'
GROUP BY nama;
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
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 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'
I have 2 tables, CAR_1 and CAR_2. Table CAR_1 contains more columns and more records than CAR_2.
Running the query below returns 4647 records:
(SELECT CAR_1.ID FROM CAR_1, CAR_2 WHERE
CAR_1.ID = CAR_2.ID AND
CAR_1.MODEL = CAR_2.MODEL AND
CAR_1.SYMBOL = CAR_2.SYMBOL AND
CAR_1.MAKE = CAR_2.MAKE AND
CAR_1.ORIGIN = CAR_2.ORIGIN) AS table_all
While running the same query without the last 2 columns comparison returns 4600 records.
(SELECT CAR_1.ID FROM CAR_1, CAR_2 WHERE
CAR_1.ID = CAR_2.ID AND
CAR_1.MODEL = CAR_2.MODEL AND
CAR_1.SYMBOL = CAR_2.SYMBOL ) AS table_min
I would like to to get the 47 records whose make and origin are different, so i tried the sql below but it does not seem to be working properly
select * from (SELECT CAR_1.ID FROM CAR_1, CAR_2 WHERE
CAR_1.ID = CAR_2.ID AND
CAR_1.MODEL = CAR_2.MODEL AND
CAR_1.SYMBOL = CAR_2.SYMBOL AND
CAR_1.MAKE = CAR_2.MAKE AND
CAR_1.ORIGIN = CAR_2.ORIGIN) AS table_all
WHERE NOT EXISTS(
(SELECT CAR_1.ID FROM CAR_1, CAR_2 WHERE
CAR_1.ID = CAR_2.ID AND
CAR_1.MODEL = CAR_2.MODEL AND
CAR_1.SYMBOL = CAR_2.SYMBOL ) AS table_min
WHERE table_all.ID = table_min.ID
)
I would appreciate it if someone can show me the error
Isn't it as simple as:
SELECT
CAR_1.ID
FROM
CAR_1, CAR_2
WHERE
CAR_1.ID = CAR_2.ID AND
CAR_1.MODEL = CAR_2.MODEL AND
CAR_1.SYMBOL = CAR_2.SYMBOL AND
(CAR_1.MAKE <> CAR_2.MAKE OR CAR_1.ORIGIN <> CAR_2.ORIGIN);
?
SELECT CAR_1.ID FROM CAR_1
outer join CAR_2 On
CAR_1.ID = CAR_2.ID
AND CAR_1.MODEL = CAR_2.MODEL
AND CAR_1.SYMBOL = CAR_2.SYMBOL
AND CAR_1.MAKE = CAR_2.MAKE
AND CAR_1.ORIGIN = CAR_2.ORIGIN
Where Car2.id is null
Will give you the id's of all cars in car1 with no matching record in Car2
More efficient than exists, or not in subqueries.