I have tow tables
tests_sub ,tests_sub_sub
and tests_sub.id = tests_sub_sub.id
so i want to delete data from both tables with one
sql query
I used the following inner join
DELETE tests_sub, tests_sub_sub FROM tests_sub
INNER JOIN tests_sub_sub ON tests_sub_sub.id = tests_sub.id
WHERE tests_sub.id = 10
the query works ok only if both tables have entries for the tests_sub.id...
( if tests_sub_sub has no entry for test_sub.id = 10 ... although the table tests_sub has entries for that id no rows are affected ...
please suggest some tips....
use LEFT JOIN.
DELETE tests_sub, tests_sub_sub
FROM tests_sub
LEFT JOIN tests_sub_sub
ON tests_sub_sub.id = tests_sub.id
WHERE tests_sub.id = 10
Related
DELETE *
FROM ((disease
INNER JOIN dishead ON disease.heading = dishead.hid)
INNER JOIN disdes ON disease.description = disdes.did)
where disease.id = 9;
basically i have inserted the data using inner join and the data is stored against same product in multiple tables
Now i am trying to write query for deleting that inserted rows from all tables
You have to specify the tables to delete from
DELETE d, dh, dd
FROM disease AS d
JOIN dishead AS dh ON d.heading = dh.hid
JOIN disdes AS dd ON d.description = dd.did
WHERE d.id = 9
Note also that if these are foreign keys, you can configure them with ON DELETE CASCADE. Then you only have to delete from the parent table, and the rows that reference them will be deleted automatically.
I'm using this query on mysql:
SELECT
`sitio`.`id_unico_sitio` AS 'ID Unico',
`sitio`.`nombre` AS 'Nombre',
`departamentos`.`departamento` AS 'Departamento',
`municipios`.`municipio` AS 'Municipio',
`sitio`.`direccion` AS 'Direccion',
`sitio`.`coor_x` AS 'Latitud',
`sitio`.`coor_y` AS 'Longitud',
`operador`.`nombre` AS 'Operador',
`tipo_tec`.`tecnologia` AS 'Tecnologia',
`sitio`.`ancho_banda` AS 'Ancho Banda(Mb/s)',
`sitio`.`fecha_inst` AS 'Fecha Instalacion',
`sitio`.`cod_sace` AS 'Cod SACE',
`sitio`.`cod_cnt` AS 'Cod CONATEL'
FROM ((((((((`sitio`
INNER JOIN `tipo_tec_sitio`
ON `sitio`.`id_unico_sitio` = `tipo_tec_sitio`.`id_unico_sitio`)
INNER JOIN `tipo_tec`
ON `tipo_tec_sitio`.`id_tipo_tec` = `tipo_tec`.`id_tipo_tec`)
INNER JOIN `administracion`
ON `sitio`.`id_administracion` = `administracion`.`id_administracion`)
INNER JOIN `estatus_finalizado`
ON `sitio`.`id_estatus_finalizado` = `estatus_finalizado`.`id_estatus_finalizado`)
INNER JOIN `departamentos`
ON `sitio`.`id_departamento` = `departamentos`.`id_departamento`)
INNER JOIN `municipios`
ON `sitio`.`id_municipio` = `municipios`.`id_municipio`)
INNER JOIN `sitios_has_operador`
ON `sitio`.`id_unico_sitio` = `sitios_has_operador`.`id_unico_sitio`)
INNER JOIN `operador`
ON `sitios_has_operador`.`id_operador` = `operador`.`id_operador`)
The problem is that with that query I'm only getting the first 2 rows when on the database there are five rows.
Is there something wrong or something that limits the number of rows returned from the database on that query?
Since you are using INNER JOIN for all the tables, are you sure you have all five rows inside all the tables ? if one of the table only contains two rows, then at the end it will only show two rows.
Make sure that your INNER JOIN condition matches all the five rows in the table you need.
I need all records from the URA table, joined on dobavljac for name of the dobavljac and all records from DOSTAVNICA table based on dostavnica.ura_id WHERE EXIST ura_id Join on gradilista for the name of gradilista.
Query:
$sql = "SELECT
ura.id,
ura.id_dobavljac,
ura.broj_racuna,
dobavljaci.id_dobavljac,
dobavljaci.naziv as dnaziv,
dobavljaci.oib,
dobavljaci.adresa,
dostavnica.ura_id,
dostavnica.id_dostavnica,
dostavnica.id_gradilista,
gradilista.id,
gradilista.naziv
FROM ura
INNER JOIN ura ON ura.id = dostavnica.ura_id
LEFT JOIN ura ON ura.id_dobavljac = dobavljaci.id_dobavljac
LEFT JOIN dostavnica ON dostavnica.id_gradilista = gradilista.id
";
Table schema:
and this is what I expect:
Try this one : A brief table structure would have helped. But based on the col names you have in the query I have posted my answer.
SELECT
ura.id,
ura.id_dobavljac,
ura.broj_racuna,
dobavljaci.id_dobavljac,
dobavljaci.naziv as dnaziv,
dobavljaci.oib,
dobavljaci.adresa,
dostavnica.ura_id,
dostavnica.id_dostavnica,
dostavnica.id_gradilista,
gradilista.id,
gradilista.naziv
FROM ura
INNER JOIN dobavljac ON ura.id_dobavljac = dobavljaci.id_dobavljac
INNER JOIN dostavnica ON ura.id = dostavnica.ura_id
WHERE EXISTS
(SELECT 1 from gradilista WHERE dostavnica.id_gradilista = gradilista.id)
Is there any workaround to get unique rows which are in zc_orders table? I only need comments field from zc_zc_orders_status_history (which have duplicate rows). This is my query:
SELECT
zc_orders.orders_id,
zc_orders_status_history.comments,
zc_orders_status_history.orders_status_id,
zc_customers.customers_email_address,
zc_customers.customers_telephone,
zc_customers.customers_firstname,
zc_customers.customers_lastname,
zc_orders_status_history.date_added,
zc_orders.date_purchased
FROM
zc_customers
INNER JOIN zc_orders ON zc_orders.customers_id = zc_customers.customers_id
INNER JOIN zc_orders_status_history ON
zc_orders_status_history.orders_id = zc_orders.orders_id
where zc_orders_status_history.orders_status_id = 8
ORDER BY zc_orders_status_history.date_added DESC
SELECT DISTINCT
zc_orders_status_history.comments,
FROM
zc_customers
INNER JOIN zc_orders ON zc_orders.customers_id = zc_customers.customers_id
INNER JOIN zc_orders_status_history ON
zc_orders_status_history.orders_id = zc_orders.orders_id
where zc_orders_status_history.orders_status_id = 8
ORDER BY zc_orders_status_history.date_added DESC
I am having 4 tables.
I want to update all table for that i am using joins.
Query is working only if all tables have values. If any one table does not have record for that than it is not updating other tables
MY query is
update post_message_users
LEFT JOIN post_messages
ON post_message_users.messageid = post_messages.messageid
LEFT JOIN comments
ON post_message_users.messageid = comments.comment_on
LEFT JOIN likes
ON post_message_users.messageid =likes.element_id
SET post_message_users.status='deleted',
post_message_users.deleted_time=NOW(),
post_messages.status = 'deleted' ,
post_messages.delete_time = now(),
comments.status ='deleted',
likes.status='deleted',
likes.delete_time=now()
WHERE
comments.element_type ='Message'
AND
likes.element_type ='Message'
and post_message_users.messageid = 33
and post_message_users.received_by= 3
Please see what change should I make so that i work properly.
Your where clause is undoing the left outer join, turning it into an inner join. To fix this, move the conditions into the on clause:
update post_message_users
LEFT JOIN post_messages
ON post_message_users.messageid = post_messages.messageid
LEFT JOIN comments
ON post_message_users.messageid = comments.comment_on and
comments.element_type = 'Message'
LEFT JOIN likes
ON post_message_users.messageid = likes.element_id and
likes.element_type = 'Message'
SET post_message_users.status='deleted',
post_message_users.deleted_time=NOW(),
post_messages.status = 'deleted' ,
post_messages.delete_time = now(),
comments.status ='deleted',
likes.status='deleted',
likes.delete_time=now()
WHERE post_message_users.messageid = 33 and post_message_users.received_by= 3;