SELECT *
FROM `tbl_schedule_task` AS `E`
JOIN `tbl_schedule` AS `S` ON `S`.`schedule_id`=`E`.`schedule_id`
JOIN `tbl_schedule_frequency` AS `F` ON `F`.`frequency_id`=`S`.`frequency_id`
JOIN `tbl_equipments` AS `M` ON `M`.`equipment_id`=`E`.`equipment_id`
LEFT JOIN `tbl_schedule_checklist` AS `L` ON `L`.`check_list_id`=`E`.`check_list_id`
LEFT JOIN `tbl_tech_groups` AS `G` ON `G`.`group_id` = `S`.`shedule_assign_id`
LEFT JOIN `tbl_tech_technicians` AS `T` ON `T`.`techgroup_id`=`G`.`group_id`
LEFT JOIN `tbl_schedule_category` AS `SC` ON `SC`.`category_id`=`S`.`category_id`
JOIN `tbl_site_users` AS `U` ON `U`.`user_id`=`E`.`created_by`
WHERE `E`.`site_id` = '1'
AND (`E`.`approve_flg` =0
AND `E`.`check_out` = 1)
AND `E`.`approve_by` = '2'
ORDER BY `E`.`task_id` DESC
May be inner joins is returning the duplicate data. Check the inner joins before left joins of above query.
Related
I want to sort data by price. but I have 2 column data tables namely base_price and discount_value. I want to sort it out by comparing it if there is a discount value and the small value will be sorted. but in this problem I can only sort according to discount_value or base_price does not combine both. is there a way to overcome it?
Query :
select `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`base_price`, `pd`.`discount_value`, `p`.`weight`, `c`.`category`, pc.id as product_color_id, `pc`.`base_color`, `i`.`image`, sum(ps.stock) as stock, `p`.`status` from `products` as `p`
left join `category_product` as `cp` on `p`.`id` = `cp`.`product_id`
left join `categories` as `c` on `cp`.`category_id` = `c`.`id`
left join `product_colors` as `pc` on `p`.`id` = `pc`.`product_id`
left join `image_product_color` as `ipc` on `ipc`.`id` = (select ipc1.id from image_product_color as ipc1 where pc.id = ipc1.product_color_id order by ipc1.id desc limit 1)
left join `images` as `i` on `i`.`id` = (select i1.id from images as i1 where ipc.image_id = i1.id order by i1.id desc limit 1)
left join `product_sizes` as `ps` on `pc`.`id` = `ps`.`product_color_id`
left join `product_discounts` as `pd` on `pd`.`id` = (select pd1.id from product_discounts as pd1 where p.id = pd1.product_id and date(now()) <= pd1.valid_until)
group by `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`base_price`, `pd`.`discount_value`, `p`.`weight`, `c`.`category`, `pc`.`id`, `pc`.`base_color`, `i`.`image`, `p`.`status`
order by CASE WHEN pd.discount_value = null THEN p.base_price ELSE pd.discount_value END ASC
Result :
Change your ORDER BY clause to the following:
ORDER BY IFNULL(pd.discount_value, p.base_price) ASC
This is how your revised query looks like:
select `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`base_price`, `pd`.`discount_value`, `p`.`weight`, `c`.`category`, pc.id as product_color_id, `pc`.`base_color`, `i`.`image`, sum(ps.stock) as stock, `p`.`status` from `products` as `p`
left join `category_product` as `cp` on `p`.`id` = `cp`.`product_id`
left join `categories` as `c` on `cp`.`category_id` = `c`.`id`
left join `product_colors` as `pc` on `p`.`id` = `pc`.`product_id`
left join `image_product_color` as `ipc` on `ipc`.`id` = (select ipc1.id from image_product_color as ipc1 where pc.id = ipc1.product_color_id order by ipc1.id desc limit 1)
left join `images` as `i` on `i`.`id` = (select i1.id from images as i1 where ipc.image_id = i1.id order by i1.id desc limit 1)
left join `product_sizes` as `ps` on `pc`.`id` = `ps`.`product_color_id`
left join `product_discounts` as `pd` on `pd`.`id` = (select pd1.id from product_discounts as pd1 where p.id = pd1.product_id and date(now()) <= pd1.valid_until)
group by `p`.`id`, `p`.`name`, `p`.`product_code`, `p`.`base_price`, `pd`.`discount_value`, `p`.`weight`, `c`.`category`, `pc`.`id`, `pc`.`base_color`, `i`.`image`, `p`.`status`
ORDER BY IFNULL(pd.discount_value, p.base_price) ASC;
This is my query:
UPDATE
`product_pricing`
SET `formula_id`= '2'
WHERE 1
SELECT
product_pricing.id,
product_pricing.formula_id,
product_pricing.vat_calculated,
products_ids.id,
products_ids.link_id,
product_attributes.fty_id,
product_attributes.size_id,
product_sizes.size_id,
product_sizes.wheel
FROM product_pricing
LEFT JOIN products_ids ON product_pricing.id = products_ids.id
LEFT JOIN product_attributes ON products_ids.link_id = product_attributes.fty_id
LEFT JOIN product_sizes ON product_attributes.size_id = product_sizes.size_id
WHERE
product_sizes.wheel = '13'
How would I incorporate the SELECT query with the update query?
I need to only update products that have product_sizes.wheel = '13' but to get this information, I need a few left joins.
You can use this update with LEFT JOIN and Where
update product_pricing
LEFT JOIN products_ids ON product_pricing.id = products_ids.id
LEFT JOIN product_attributes ON products_ids.link_id = product_attributes.fty_id
LEFT JOIN product_sizes ON product_attributes.size_id = product_sizes.size_id
SET product_pricing.formula_id= '2'
WHERE
product_sizes.wheel = '13'
I think an INNER JOIN is needed here:
update product_pricing
INNER JOIN products_ids ON product_pricing.id = products_ids.id
INNER JOIN product_attributes ON products_ids.link_id = product_attributes.fty_id
INNER JOIN product_sizes ON product_attributes.size_id = product_sizes.size_id
SET product_pricing.formula_id= '2'
WHERE product_sizes.wheel = '13'
try this
update product_pricing p
LEFT JOIN products_ids pi ON p.id = pi.id
LEFT JOIN product_attributes pa ON pi.link_id = pa.fty_id
LEFT JOIN product_sizes ps ON pa.size_id = ps.size_id
SET p.formula_id= '2'
WHERE
ps.wheel = '13'
I have a query that contains a sub-query and many Left Outer Joins. Within my sub-query, I am ordering by records by the most recent blog_date and limiting the results by 10 records. This sub-query should dictate the order of the records with the other joins matching up additional information about that record, however, when I add in the LEFT OUTER JOIN it disregards the blog_date ordering. This has led me to believe that I am either missing a key element to persisting the ordering or that LEFT OUTER JOIN is not the right join to be using.
Here is my full query:
SELECT `b`.`blog_id`, `b`.`blog_date`,`b`.`title`, `u`.`user_id`, `u`.`first_name`, `c`.`category_name`, `d`.`discovery_source_name`, `bc`.`comment`, `bf`.`file`
FROM (SELECT * FROM `blog` ORDER BY `blog`.`blog_date` DESC limit 10) `b`
LEFT OUTER JOIN `user` `u` ON `b`.`user_id` = `u`.`user_id` AND `u`.`organization_id` = 1
LEFT OUTER JOIN `category` `c` ON `b`.`category_id` = `c`.`category_id`
LEFT OUTER JOIN `discovery_source` `d` ON `b`.`discovery_source_id` = `d`.`discovery_source_id`
LEFT OUTER JOIN `blog_comment` `bc` ON `b`.`blog_id` = `bc`.`blog_id`
LEFT OUTER JOIN `blog_file` `bf` ON `b`.`blog_id` = `bf`.`blog_id`
;
Here are the results when I just include the first join (user) the records are in the correct order (2017-02-21 most recent):
However, when I add in the second left outer join (and remaining) it appears that the new order is in descending blog_date order, but then grouped by category_name.
Ordering the subquery has no effect on the order of your outer query. Put the ORDER BY at the end of the outer query.
SELECT `b`.`blog_id`, `b`.`blog_date`,`b`.`title`, `u`.`user_id`, `u`.`first_name`, `c`.`category_name`, `d`.`discovery_source_name`, `bc`.`comment`, `bf`.`file`
FROM (SELECT * FROM `blog` ORDER BY `blog`.`blog_date` DESC limit 10) `b`
LEFT OUTER JOIN `user` `u` ON `b`.`user_id` = `u`.`user_id` AND `u`.`organization_id` = 1
LEFT OUTER JOIN `category` `c` ON `b`.`category_id` = `c`.`category_id`
LEFT OUTER JOIN `discovery_source` `d` ON `b`.`discovery_source_id` = `d`.`discovery_source_id`
LEFT OUTER JOIN `blog_comment` `bc` ON `b`.`blog_id` = `bc`.`blog_id`
LEFT OUTER JOIN `blog_file` `bf` ON `b`.`blog_id` = `bf`.`blog_id`
ORDER BY `b`.`blog_date` DESC ;
I have issues with this query. I think the issue is with not finding the items. Afterwards I am doing LEFT OUTER JOIN on it but before that nothing. What is the best solution to include the items table in the beginning ?
SELECT COUNT(*) AS `numrows` FROM (`categories_items`)
LEFT OUTER JOIN `items_stones` `items_stones` ON `items`.`id` = `items_stones`.`item_model_id`
LEFT OUTER JOIN `items` ON `items`.`id` = `categories_items`.`item_model_id`
WHERE ( `items_stones`.`stone_model_id` = 1 ) AND `categories_items`.`category_model_id` = 1
Change your query to be like below, basically swap the LEFT JOINS
SELECT COUNT(*) AS `numrows`
FROM `categories_items`
LEFT OUTER JOIN `items` ON `items`.`id` = `categories_items`.`item_model_id`
AND `categories_items`.`category_model_id` = 1
LEFT OUTER JOIN `items_stones` ON `items`.`id` = `items_stones`.`item_model_id`
AND `items_stones`.`stone_model_id` = 1;
Here's a fiddle with the schema and a sample query: http://sqlfiddle.com/#!2/573ec4/9
I'm looking to return C.price using I.section and L.level, which are obtained via other joins. When using an inner join with Cost I am left with no result: http://sqlfiddle.com/#!2/573ec4/2
Since cost is a table that maps a section and level to a price, I would like to be able to calculate the price of all the showings in my query
When you are joining to the Cost table you do not include the proper join conditions:
INNER JOIN `cost` `C`
ON `I`.`Section` = c.section -- = c.section is missing
AND `L`.`level` = c.level; -- = c.level is missing
So your full query will be:
SELECT `F`.`date`,
`F`.`time`,
`F`.`tname`,
`I`.`section`,
`L`.`level`,
c.price
FROM `booking_for_schedule` `F`
INNER JOIN `booking_in_seats` `I`
on `F`.`tname`=`I`.`tname`
AND `F`.`booking_num` = `I`.`booking_num`
INNER JOIN `level` `L`
on `F`.`date`=`L`.`date`
AND `F`.`time`=`L`.`time`
AND `F`.`tname`=`L`.`tname`
INNER JOIN `cost` `C`
ON `I`.`Section` = c.section
AND `L`.`level` = c.level;
See SQL Fiddle with Demo
Looks like you're missing some clause on the INNER JOIN cost C (when you are joining on the Cost table).
It should look like this:
INNER JOIN `cost` `C` ON `I`.`Section`=`C`.`Section` AND `L`.`level`=`C`.`level`
instead of:
INNER JOIN `cost` `C` ON `I`.`Section` AND `L`.`level`
You have missed to join the columns. the reason why you have no result is because I.Section AND L.level will always return false.
SELECT F.date,
F.time,
F.tname,
I.section,
L.level,
c.*
FROM booking_for_schedule F
INNER JOIN booking_in_seats I
ON F.tname = I.tname AND
F.booking_num = I.booking_num
INNER JOIN level L
ON F.date = L.date AND
F.time = L.time AND
F.tname = L.tname
INNER JOIN cost C
ON I.Section = C.Section AND -- <<== HERE
L.level = C.level -- <<== HERE
SQLFiddle Demo
Your last line is the problem:
INNER JOIN `cost` `C` ON `I`.`Section` AND `L`.`level`
It should be this:
INNER JOIN `cost` `C` ON `I`.`Section` = `C`.`Section` AND `L`.`level` = `C`.`Level`