Update Issue in Subquery - mysql

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 = ''

Related

How can I insert the id number, of the p param, in a query

This is the param and the query, I need to insert this number in the WHERE of the query. The #p pf the where, doesn´t recieve it
def get_stock
#p = Proveedor.find(params[:id])
sql5 = "SELECT productos.id as producto,
productos.nombre as nombre,
productos.stock as cantidad
FROM proveedors
INNER JOIN productos ON proveedors.id = productos.proveedor_id
WHERE proveedors.id = #p
GROUP BY productos.id"
#aux = ActiveRecord::Base.connection.execute(sql5)
end
You can use following. This will suffice your request.
def get_stock
#p = Proveedor.find(params[:id])
sql5 = "SELECT productos.id as producto,
productos.nombre as nombre,
productos.stock as cantidad
FROM proveedors
INNER JOIN productos ON proveedors.id = productos.proveedor_id
WHERE proveedors.id = #{#p.try(:id)}
GROUP BY productos.id"
#aux = ActiveRecord::Base.connection.execute(sql5)
end
Try
sql5 = "SELECT productos.id as producto,
productos.nombre as nombre,
productos.stock as cantidad
FROM proveedors
INNER JOIN productos ON proveedors.id = productos.proveedor_id
WHERE proveedors.id = #{#p.id}
GROUP BY productos.id"
Hope it helps.

How to query multiple Left Join table when there exist null

I have 20 tables (list of subjects) that has the same table structure as below.
columns
|id|student_id|result_session|result_term|result_arm|result_class|result_level|1st_test|2nd_test|exam|result_total|result_grade|result_remark|
I then have another table result_summary as below:
|id|student_id|result_score|result_average|result_term|result_session|result_level|result_class|result_arm
Minimal Query
When I write my query like so:
SELECT t2.`1st_test` AS agric1, t2.`2nd_test` AS agric2, t2.exam AS agricExam3, t2.result_total AS agricTotal,
t2.result_grade AS agricGrade, t3.`1st_test` AS basicsci1, t3.`2nd_test` AS basicsci2, t3.exam AS basicsciExam,
t3.result_total AS basicsciTotal, t3.result_grade AS basicsciGrade t2.result_remark AS agricRemark, t3.result_remark AS basicSciRemark
FROM ((result_summary AS t1 LEFT JOIN
agricultural_science AS t2 ON t1.student_id = t2.student_id) LEFT JOIN
basic_science AS t3 ON t1.student_id = t3.student_id)
WHERE (t1.student_id = '#studentID' AND t1.result_term = '#term' AND t1.result_session = '#session' AND t1.result_level = '#level' AND t1.result_class = '#resultClass') AND
(t2.student_id = '#studentID' AND t2.result_term = '#term' AND t2.result_session = '#session' AND t2.result_level = '#level' AND t2.result_class = '#resultClass') AND
(t3.student_id = '#studentID' AND t3.result_term = '#term' AND t3.result_session = '#session' AND t3.result_level = '#level' AND t3.result_class = '#resultClass')
It does not return any row. Mind you, there are data matching the query data in table t1 and t2 but not in t3.
How can I write the query to get the desire result? Thanks in advance.
Defining t2 and t3 table's conditions in where clause turns your left joins into INNER JOIN.
The t2 and t3 table's conditions will need to be defined in ON instead of WHERE:
select t2.`1st_test` as agric1,
t2.`2nd_test` as agric2,
t2.exam as agricExam3,
t2.result_total as agricTotal,
t2.result_grade as agricGrade,
t3.`1st_test` as basicsci1,
t3.`2nd_test` as basicsci2,
t3.exam as basicsciExam,
t3.result_total as basicsciTotal,
t3.result_grade as basicsciGrade t2.result_remark as agricRemark,
t3.result_remark as basicSciRemark
from (
(
result_summary as t1 left join agricultural_science as t2 on t1.student_id = t2.student_id
) left join basic_science as t3 on t1.student_id = t3.student_id
and (
t3.student_id = '#studentID'
and t3.result_term = '#term'
and t3.result_session = '#session'
and t3.result_level = '#level'
and t3.result_class = '#resultClass'
)
and (
t2.student_id = '#studentID'
and t2.result_term = '#term'
and t2.result_session = '#session'
and t2.result_level = '#level'
and t2.result_class = '#resultClass'
)
)
where (
t1.student_id = '#studentID'
and t1.result_term = '#term'
and t1.result_session = '#session'
and t1.result_level = '#level'
and t1.result_class = '#resultClass'
);

Multiple rows in a select with OR

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.

How to update two columns from one select

Can I combine this in to one?
UPDATE 1_packages k
SET
rrp = (SELECT
SUM(u.quantity * p.rrp) * (sum(p.price_each <> 0 and p.rrp = 0) = 0)
FROM
1_packages_plu u
INNER JOIN
1_products p ON u.fk_products_id = p.id
WHERE
fk_packages_id = k.id)
;
UPDATE 1_packages k
SET
rrp_inc = (SELECT
SUM(u.quantity * p.rrp_inc) * (sum(p.price_each <> 0 and p.rrp_inc = 0) = 0)
FROM
1_packages_plu u
INNER JOIN
1_products p ON u.fk_products_id = p.id
WHERE
fk_packages_id = k.id)
;
It's simple, Try
UPDATE 1_packages k
SET rrp = (SELECT SUM(u.quantity * p.rrp) * (sum(p.price_each <> 0 and p.rrp = 0) = 0)
FROM 1_packages_plu u INNER JOIN 1_products p ON u.fk_products_id = p.id
WHERE fk_packages_id = k.id),
rrp_inc = (SELECT SUM(u.quantity * p.rrp_inc) * (sum(p.price_each <> 0 and p.rrp_inc = 0) = 0)
FROM 1_packages_plu u INNER JOIN 1_products p ON u.fk_products_id = p.id
WHERE fk_packages_id = k.id)
Try :
UPDATE table_name
SET column_one = "value one",
column_two = "value two"
WHERE condition

Target table not Updatable error

I need to run this query :
UPDATE (
SELECT r.*
FROM booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON (r.st_from = new.from_t)
and (r.st_to = new.to_t)
and r.date = '2013-04-16'
) temp
SET temp.seat_ac = temp.seat_ac-5
but on execution it gives an error:
#1288-The target table temp of the UPDATE is not updatable.
Any solutions?
I think your UPDATE syntax is incorrect. See if this works:
UPDATE booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON r.st_from = new.from_t
and r.st_to = new.to_t
and r.date = '2013-04-16'
SET r.seat_ac = r.seat_ac-5