I have 2 mysql tables called tbl_flats and tbl_blocks.
I'm joining these two tables like this:
select
`f`.`fld_id` AS `fld_flat_id`,
`b`.`fld_block_name`,
`f`.`fld_mobile_app_password`,
`f`.`fld_site_id`
from
`tbl_blocks` `b`
left join
`tbl_flats` `f` ON (`f`.`fld_block_id` = `b`.`fld_id`)
where
`f`.`fld_site_id` = 57
It displays like this:
Now what I'm trying to do is update fld_mobile_app_password columns to NULL. Here's what I've tried but failed:
update (select
`f`.`fld_id` AS `fld_flat_id`,
`b`.`fld_block_name`,
`f`.`fld_mobile_app_password`,
`f`.`fld_site_id`
from
`tbl_blocks` `b`
left join
`tbl_flats` `f` ON (`f`.`fld_block_id` = `b`.`fld_id`) )
set
`f`.`fld_mobile_app_password` = '1a1dc91c907325c69271ddf0c944bc72'
where
`f`.`fld_site_id` = 57
I can't seem to figure out the problem. Any tip is appriciated
You can't update columns in a subquery in MySQL. Just remove the subquery:
update `tbl_blocks` `b` join
`tbl_flats` `f`
on `f`.`fld_block_id` = `b`.`fld_id`
set `f`.`fld_mobile_app_password` = '1a1dc91c907325c69271ddf0c944bc72'
where `f`.`fld_site_id` = 57;
An inner join should be sufficient here -- the where clause turns the outer join into an inner join anyway.
Related
I am using the laravel to build my web app. In that for listing all orders I have an query but it takes upto 40s in my localhost whereas in goddady shared hosting it takes upto 95s. Although I implemented the server side datatable yet the query takes bit long to execute. I need someone to suggest or help to make the query to excute more efficient. There are majorly two sets and I make that union.
select * from ((
select `subscription`.`id`,
`subscription`.`order_id`,
`users`.`name` as `user_name`,
`subscription`.`new_dated`,
`subscription`.`rescheduling_delivery_date` as expected_date,
COUNT(`subscription_items`.`product_id`) as items,
`subscription`.`od_dis_total` as `total`,
`subscription`.`order_discount`,
`area`.`area_name`,
`subscribe_orders`.`action` as ordertype,
driver.name as driver_name,
'NA' as payment_mode,
`payment_status`.`status_name` as payment_status,
`subscription_status`.`status_name` as order_status,
`subscription`.`od_payment_method`,
`subscription_items`.`product_id`,
subscription.delivered_dated
from `subscription`
left join `subscription_items` on `subscription`.`id` = `subscription_items`.`subscription_id`
left join `users` on `subscription`.`user_id` = `users`.`id`
left join `users` as `driver` on `subscription`.`driver_id` = `driver`.`id`
left join `ycias_address` on `subscription`.`add_id` = `ycias_address`.`id`
left join `area` on `ycias_address`.`area_id` = `area`.`id`
left join `subscribe_order_items` on `subscription`.`subscribe_order_id` = `subscribe_order_items`.`subscribe_order_id`
left join `subscribe_orders` on `subscription`.`subscribe_order_id` = `subscribe_orders`.`id`
left join `payment_status` on `subscription`.`payment_status` = `payment_status`.`id`
left join `subscription_status` on `subscription`.`od_status` = `subscription_status`.`id`
group by `subscription_items`.`subscription_id`)
union (
select `orders`.`id`,
`orders`.`order_id`,
`users`.`name` as `user_name`,
`orders`.`new_dated`,
`orders`.`expected_delivery_date` as expected_date,
COUNT(order_items.product_id) as items,
`orders`.`od_dis_total` as `total`,
`orders`.`order_discount`,
`area`.`area_name`,
'Instant Order' as ordertype,
driver.name as driver_name,
orders.payment_mode,
`payment_status`.`status_name` as payment_status,
`order_status`.`status_name` as order_status,
`orders`.`od_payment_method`,
`order_items`.`product_id`,
`orders`.`delivered_dated`
from `orders`
left join `order_items` on `orders`.`id` = `order_items`.`order_id`
left join `users` on `users`.`id` = `orders`.`user_id`
left join `users` as `driver` on `driver`.`id` = `orders`.`driver_id`
left join `ycias_address` on `orders`.`add_id` = `ycias_address`.`id`
left join `area` on `ycias_address`.`area_id` = `area`.`id`
left join `payment_status` on `orders`.`payment_status` = `payment_status`.`id`
left join `order_status` on `orders`.`od_status` = `order_status`.`id`
group by `order_items`.`order_id`)) as all_orders limit 10 offset 0
As per the suggestion given by Andy Lester I have added the index for the necessary columns. So the execution time is reduced to half that is 18 secs in localhost. I further investigated that the index columns are of datatype VARCHAR. So I changed that to INT and then executed, got results within 0.9 secs. Thanks to Andy Lester.
This is the query where I need the results to return only rows with the userID = 12345678 the result contains also fields with other userID
select `a`.*, `b`.`someField` from `a` left join `b` on `a`.`userID` = '12345678'
How can I solve this?
You are using the JOIN condition in the wrong way:
SELECT `a`.*, `b`.`someField`
from `a` left join `b` on a.matchingField = b.matchingField
WHERE `a`.`userID` = '12345678'
This is the way to do a proper join. You have a field that is the matchingField that is the link between the records in one table and in the other and the userID is the filtering condition.
Note that macthingField can also be the same userID. In that case:
SELECT `a`.*, `b`.`someField`
from `a` left join `b` on a.userID = b.userID
WHERE `a`.`userID` = '12345678'
I have this query, which works fine:
select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
So, it's a query on table 1 with a join on table 2, then subsequent joins from multiple aliased joins of table 3 on table 2, but as soon as I add further joins, I get no results and I'm not sure why, for example:
select table_1.*, coalesce(test_1.type, test_2.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
inner join `table_3` as `test_2` on `test_2`.`code` = `table_2`.`column` and `table_2`.`column` = 'H'
Can anyone explain what I have done wrong?
Try LEFT join on table_3 . If there are no records for table_3, that's why you yield no results, due to the INNER join.
And actually, you're not joining any columns on table_3. Is most likely the issue.
What is the expected sample result of the second of your query ?
Could you please try this query ?
select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and
(`table_2`.`column` = 'L' or `table_2`.`column` = 'H')
I have the following SQL. I am trying to perform a GROUP_CONCAT within a Join but no matter how I seem try it doesnt seem to work right.
Essentially there is a link tag table that does a one to many on another table that holds tags. So there might be say 8 tags for one article record. I need them concatenated together .vs. ending up with 8 records.
SELECT
`articles`.`art_title`,
`articles`.`art_bodytext`,
`info`.`inf_start_date`,
`info`.`inf_end_date`,
`info`.`inf_hits`,
`info`.`inf_acl`,
`category`.`name`,
`options`.`opt_tpl`,
`options`.`opt_published`,
`options`.`opt_options`,
`options`.`opt_acl`,
`tags`.`tag`
FROM
`linktable`
INNER JOIN `articles` ON (`linktable`.`lnk_data_id` = `articles`.`art_id`)
INNER JOIN `info` ON (`articles`.`art_info_id` = `info`.`inf_id`)
INNER JOIN `category` ON (`articles`.`art_cat_id` = `category`.`id`)
AND (`articles`.`art_cat_tree_id` = `category`.`fid`)
INNER JOIN `options` ON (`articles`.`art_opt_id` = `options`.`opt_id`)
INNER JOIN `linktags` ON (`articles`.`art_tag_set_id` = `linktags`.`lnk_tagset_id`)
LEFT OUTER JOIN GROUP_CONCAT(`tags`) ON (`linktags`.`lnk_tag_id` = `tags`.`tag_id`)
WHERE
`linktable`.`lnk_pgc_id` = 1
Move the GROUP_CONCAT() into the field list, it is not a join condition, but a field calculation.
Add a GROUP BY to find the rows to combine
This gives
SELECT
`articles`.`art_title`,
`articles`.`art_bodytext`,
`info`.`inf_start_date`,
`info`.`inf_end_date`,
`info`.`inf_hits`,
`info`.`inf_acl`,
`category`.`name`,
`options`.`opt_tpl`,
`options`.`opt_published`,
`options`.`opt_options`,
`options`.`opt_acl`,
GROUP_CONCAT(`tags`.`tag`) AS tags
FROM
`linktable`
INNER JOIN `articles` ON (`linktable`.`lnk_data_id` = `articles`.`art_id`)
INNER JOIN `info` ON (`articles`.`art_info_id` = `info`.`inf_id`)
INNER JOIN `category` ON (`articles`.`art_cat_id` = `category`.`id`)
AND (`articles`.`art_cat_tree_id` = `category`.`fid`)
INNER JOIN `options` ON (`articles`.`art_opt_id` = `options`.`opt_id`)
INNER JOIN `linktags` ON (`articles`.`art_tag_set_id` = `linktags`.`lnk_tagset_id`)
LEFT JOIN `tags` ON (`linktags`.`lnk_tag_id` = `tags`.`tag_id`)
WHERE
`linktable`.`lnk_pgc_id` = 1
GROUP BY `articles`.`art_id`
This is my current query:
$sel = "SELECT
db1t1.userid, db1t1.customer_id, db2t1.customers_id, db2t1.orders_id, db2t2.products_price
FROM
database1.table1 db1t1
LEFT JOIN database2.table1 db2t1 ON
db1t1.customer_id = db2t1.customers_id
LEFT JOIN database2.table2 db2t2 ON
db2t1.orders_id = db2t2.orders_id
WHERE db1t1.userid IN(
SELECT
l.userid, l.username, r.username, r.cus_id
FROM
database1.table3 l
LEFT JOIN database2.table4 r ON
l.username = r.username
WHERE r.cus_id = '1234'
)";
Error message:
Operand should contain 1 column(s)
The error occurred because that you returned a result with multiple columns to an IN clause.
Try this:
SELECT
`db1t1`.`userid`, `db1t1`.`customer_id`, `db2t1`.`customers_id`,
`db2t1`.`orders_id`, `db2t2`.`products_price`
FROM `database1`.`table1` AS `db1t1`
LEFT JOIN `database2`.`table1` AS `db2t1`
USING (`customers_id`)
LEFT JOIN `database2`.`table2` AS `db2t2`
USING (`orders_id`)
WHERE `db1t1`.`userid` IN (
SELECT `l`.`userid`
FROM `database1`.`table3` AS `l`
LEFT JOIN `database2`.`table4` AS `r`
USING (`username`)
WHERE `r`.`cus_id` = 1234
)
What are you trying to achieve ? Maybe we can find a better solution.
Also, I think that you should do an INNER JOIN instead of a LEFT JOIN in the subquery.