I have a table tbl_loyaltypoints in this table a column status = 2 has default value
Now I have following Query to get all records
select lp.order_id, DATEDIFF(CURDATE(), FROM_UNIXTIME(lp.created_at)) as createon,
oi.deal_id, wo.returnWithin, lp.status
from tbl_loyaltypoints lp
inner join tbl_orders ord on lp.order_id = ord.id
inner join tbl_order_item oi on lp.order_id = oi.order_id
inner join tbl_workorders wo on oi.deal_id = wo.id
where ord.order_type = 1
order by lp.id DESC;
Output:
order_id createon deal_id returnWithin status
1045 4 160 20 2
1044 4 160 20 2
1043 20 160 20 2
I want to update status tbl_loyaltypoints.status when createon==returnWithin.
Is there any way to do this using Mysql?
You can transform the select into a join:
update tbl_loyaltypoints lp inner join
tbl_orders ord
on lp.order_id = ord.id inner join
tbl_order_item oi
on lp.order_id = oi.order_id inner join
tbl_workorders wo
on oi.deal_id = wo.id
set lp.status = ??
where ord.order_type = 1 and
DATEDIFF(CURDATE(), FROM_UNIXTIME(lp.created_at)) = wo.returnWithin;
You don't specify what the new status is. The ?? is a placeholder.
Related
I have four table and structure as below :
1)Budget
id Budget_name
1 test1
2 test2
3 test3
2)Yearly Budget
id amount_yearly budgetid
1 1000 1
2 2000 2
3 5000 3
ri_spent
id Spent_amount budgetid
1 100 2
2 100 2
3 200 3
4)FI_spent
id Spent_amount budgetid
1 100 2
2 100 3
3 200 3
i want to fetch data accourding to or based on first budget table id
below is the query i was trying:
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d
inner join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
inner join RI_DETAILS as l on l.COST_CENTER = d.id
inner join F_RI_DETAILS as f on f.COST_CENTER = d.id
ORDER BY d.id DESC
I want output of the following way:
Id Name ri_id FI_ID RI_Spent_Amount FI_Spent_Amount Annual_Buget
1 test1 1000
2 test2 1 1 100 200 1000
2 test2 2 100 2000
3 test3 3 2 300 100 2000
3 test3 3 200 2000
Any way if possible then please help me.
I want to minus annual budget with spent_amount later.
If possible then help me .
Please Try This Query ,
select b.id ,
b.Budget_name,
y.id,
y.amount_yearly,
r.id,
r.ri_spent,
f.id,
f.Spent_amount
from Budget b
INNER JOIN Yearly_Budget y on y.budgetid =b.id
INNER JOIN ri_spent r on r.budgetid =b.id
INNER JOIN FI_spent f on f.budgetid =b.id
ORDER BY b.id DESC
Could you please use left join instead of inner join. Requirement and sql arent matching so i may be little off. But you can get the idea.
select d.centers as Cost_Center,
ud.BUDGET_ANNUAL_AMOUNT as Annual_Budget,
l.LEAD_ID as Lead_Id,
l.AMOUNT as Lead_Amount,
f.FINANCEADD_ID as Finance_Id,
f.AMOUNT as Finance_Amount
from Cost_centers as d -- I assume this is the budget table
left outer join ANNUAL_BUDGET_BUDGET_CENTER as ud on d.id = ud.BUDGET_ID
left outer join RI_DETAILS as l on l.COST_CENTER = d.id
left outer join
(select sum(FINANCEADD_ID) FINANCEADD_ID,sum(AMOUNT) AMOUNT,id from
F_RI_DETAILS group by id) as f
on f.COST_CENTER = l.id
ORDER BY d.id DESC
I have table with query :
SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName, TM.terminal_name,
CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) /
TOPUP.currency_rate, 2) AS decimal(18, 2)) AS
Top_Up_Value
FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP
INNER JOIN dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id =
TU.terminal_user_id
INNER JOIN dbo.cdf_currency AS CR ON TOPUP.currency_id = CR.currency_id
INNER JOIN dbo.cdf_cuid AS CU ON TOPUP.cu_id = CU.cu_id
INNER JOIN dbo.cdf_card_role AS CO ON CO.id = CU.card_role_id
INNER JOIN dbo.cdf_terminal_user_account AS UA ON UA.terminal_user_id =
TU.terminal_user_id
INNER JOIN dbo.cdf_terminal AS TM ON TM.terminal_id = UA.terminal_id
INNER JOIN dbfastshosted.dbo.fh_sales_map AS MA ON MA.tu_log_id =
TOPUP.tu_log_id
LEFT OUTER JOIN dbfastshosted.dbo.fh_mf_new_card_logs AS NC ON
MA.nc_log_id = NC.nc_log_id
WHERE (ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) > 0)
and YEAR(TOPUP.tu_timestamp) = '2017'
AND month(TOPUP.tu_timestamp) = 1
AND TM.terminal_id = 7
GROUP BY TOPUP.tu_log_id,DATENAME(Month,TOPUP.tu_timestamp),
TM.terminal_name,
TOPUP.tu_credit, NC.initial_bal, TOPUP.currency_rate, CU.card_type_id;
MonthName Terminal name Top Up Value
------------------------------------------------------
January Terminal 1 100
January Terminal 1 200
January Terminal 3 150
Feb Terminal 1 250
Feb Terminal 1 160
March Terminal 2 120
March Terminal 3 100
and i would like to have total sums of top up value according to months which look like this:
MonthName Top Up Value
-----------------------------------
January 450
February 410
March 220
-----
Dec
as i am beginner in sql , i dont have idea how to do it. Really need help on these. Thanks!
SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName,
SUM(CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) /
TOPUP.currency_rate, 2) AS decimal(18, 2))) AS
Top_Up_Value
FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP
INNER JOIN dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id =
TU.terminal_user_id
look at your group by statement, the result you posted is displaying lesser number of columns then what you have in your query. Tweak it around and you will get your results
Try this
SELECT DATENAME(Month,TOPUP.tu_timestamp) AS MonthName,
SUM(CAST(ROUND(ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) /
TOPUP.currency_rate, 2) AS decimal(18, 2))) AS
Top_Up_Value
FROM dbfastshosted.dbo.fh_mf_top_up_logs AS TOPUP
INNER JOIN dbo.cdf_terminal_user AS TU ON TOPUP.terminal_user_id =
TU.terminal_user_id
INNER JOIN dbo.cdf_currency AS CR ON TOPUP.currency_id = CR.currency_id
INNER JOIN dbo.cdf_cuid AS CU ON TOPUP.cu_id = CU.cu_id
INNER JOIN dbo.cdf_card_role AS CO ON CO.id = CU.card_role_id
INNER JOIN dbo.cdf_terminal_user_account AS UA ON UA.terminal_user_id =
TU.terminal_user_id
INNER JOIN dbo.cdf_terminal AS TM ON TM.terminal_id = UA.terminal_id
INNER JOIN dbfastshosted.dbo.fh_sales_map AS MA ON MA.tu_log_id =
TOPUP.tu_log_id
LEFT OUTER JOIN dbfastshosted.dbo.fh_mf_new_card_logs AS NC ON
MA.nc_log_id = NC.nc_log_id
WHERE (ISNULL(TOPUP.tu_credit - NC.initial_bal, TOPUP.tu_credit) > 0)
and YEAR(TOPUP.tu_timestamp) = '2017'
AND month(TOPUP.tu_timestamp) = 1
AND TM.terminal_id = 7
GROUP BY DATENAME(Month,TOPUP.tu_timestamp)
I am facing 1 issue with MySql query:
select sum(h.total_rooms) as total_rooms from reservation_details as rd LEFT JOIN hotels as h ON rd.hotel_id = h.id left join `chains` as `c` on `c`.`id` = `h`.`chain_id` where YEAR(rd.created_at) = 2016 and MONTH(rd.created_at) = 5 and rd.status >= 50 and h.chain_id = 2 GROUP BY rd.hotel_id
Above query returns :
total_rooms
48216
7700
13250
But
I need sum of the query.
Try to use sum() function like this and use ROLLUP with your query.
select sum(h.total_rooms) as total_rooms from reservation_details as rd
LEFT JOIN hotels as h ON rd.hotel_id = h.id
left join `chains` as `c` on `c`.`id` = `h`.`chain_id`
where YEAR(rd.created_at) = 2016 and MONTH(rd.created_at) = 5
and rd.status >= 50 and h.chain_id = 2 GROUP BY rd.hotel_id
WITH ROLLUP
I need help about generating query for multiple column.
part of my tbl_advert_specific_fields_values table look like:
id advert_id field_name field_value
1 654 t1_sqft 50
2 655 t1_yearbuilt 1999
3 1521 t2_doorcount 5
4 656 t1_yearbuilt 2001
5 656 t1_sqft 29
6 654 t1_yearbuilt 2004
SELECT p.*, p.id AS id, p.title AS title, usr.id as advert_user_id,
p.street_num, p.street,c.icon AS cat_icon,c.title AS cat_title,c.title AS cat_title,
p.description as description,
countries.title as country_name,
states.title as state_name,
date_FORMAT(p.created, '%Y-%m-%d') as fcreated
FROM tbl AS p
LEFT JOIN tbl_advertmid AS pm ON pm.advert_id = p.id
INNER JOIN tbl_usermid AS am ON am.advert_id = p.id
LEFT JOIN tbl_users AS usr ON usr.id = am.user_id
INNER JOIN tbl_categories AS c ON c.id = pm.cat_id
INNER JOIN tbl_advert_specific_fields_values AS asfv ON asfv.advert_id = p.id
LEFT JOIN tbl_countries AS countries ON countries.id = p.country
LEFT JOIN tbl_states AS states ON states.id = p.locstate
WHERE p.published = 1 AND p.approved = 1 AND c.published = 1
AND (asfv.field_name = 't1_yearbuilt'
AND CONVERT(asfv.field_value,SIGNED) <= 2004 )
AND (asfv.field_name = 't1_sqft'
AND CONVERT(asfv.field_value,SIGNED) <= 50)
AND p.price <= 10174945 AND (p.advert_type_id = 1)
AND (c.id = 43 OR c.parent = 43)
GROUP BY p.id
ORDER BY p.price DESC
ok, the problem is in this asfv query part that are generated dynamically. It belong to objects which represent adverts by its specific fields. asfv is actually advert_specific_fields_values table (table name say all about it).
Without part:
AND (asfv.field_name = 't1_yearbuilt'
AND CONVERT(asfv.field_value,SIGNED) <= 2004 )
AND (asfv.field_name = 't1_sqft'
AND CONVERT(asfv.field_value,SIGNED) <= 50)
query return all adverts that belong on advert_type_id and price of them are less than 10.174.945,00 €.
All what I need is query update that return only adverts, for example t1_yearbuilt less than 2005 and t1_sqft less than 51 (advert_id => 654,656).
I also need query for values between for example t1_sqft >=30 AND t1_sqft <=50 (advert_id => 654).
Can anybody know how, update this query?
TNX
I've got this query and I want to SUM all the results of the query grouped by the column omschrijving.
The query
SELECT b.BoekRegelBedrag as total, c.omschrijving, ctl.vd1, b.BoekRegelId
FROM condensations as c
LEFT JOIN condensations_to_ledgers as ctl
ON ctl.vd1 = c.code
LEFT JOIN BoekstukRegels as b
ON b.BoekRegelGrootboekNr = ctl.GrootboekNummer
LEFT JOIN GrootboekRekeningen as g
ON g.GrootboekNummer = ctl.GrootboekNummer
WHERE c.bedrijf_id = 118
AND b.BoekregelUserId = 118
AND ctl.bedrijf_id = 118
AND g.GrootboekUserId = 118
AND c.code < 10
AND g.BaSoort = 2
AND b.BoekRegelPeriode BETWEEN 201000 AND 201013
GROUP BY b.BoekRegelId
Is there a simple way to do this?
EDIT
I tried to SUM BoekRegelBedrag but then each record sums up a part in one way or the other and i got 4 results instead of one result with the total of the summed column
Since you've not clearly stipulated which column(s) should be summed, we have to guess. Assuming that the BoekRegelId column should not be summed (it seldom makes sense to do arithmetic on ID numbers) — and then not summing ctl.vd1 per comment — then:
SELECT omschrijving, SUM(total) AS sum_total
FROM (SELECT b.BoekRegelBedrag as total, c.omschrijving, ctl.vd1, b.BoekRegelId
FROM condensations AS c
LEFT JOIN condensations_to_ledgers AS ctl
ON ctl.vd1 = c.code
LEFT JOIN BoekstukRegels AS b
ON b.BoekRegelGrootboekNr = ctl.GrootboekNummer
LEFT JOIN GrootboekRekeningen AS g
ON g.GrootboekNummer = ctl.GrootboekNummer
WHERE c.bedrijf_id = 118
AND b.BoekregelUserId = 118
AND ctl.bedrijf_id = 118
AND g.GrootboekUserId = 118
AND c.code < 10
AND g.BaSoort = 2
AND b.BoekRegelPeriode BETWEEN 201000 AND 201013
GROUP BY b.BoekRegelId
) AS I
GROUP BY omschrijving;
Basically, I'm using your original query result as a 'table' in the FROM clause, and then aggregating on its columns in a way which might be what you're after.
An alternative, simpler approach may also be feasible if the core query is close to what you wanted:
SELECT c.omschrijving, SUM(b.BoekRegelBedrag) as total
FROM condensations AS c
LEFT JOIN condensations_to_ledgers AS ctl ON ctl.vd1 = c.code
LEFT JOIN BoekstukRegels AS b ON b.BoekRegelGrootboekNr = ctl.GrootboekNummer
LEFT JOIN GrootboekRekeningen AS g ON g.GrootboekNummer = ctl.GrootboekNummer
WHERE c.bedrijf_id = 118
AND b.BoekregelUserId = 118
AND ctl.bedrijf_id = 118
AND g.GrootboekUserId = 118
AND c.code < 10
AND g.BaSoort = 2
AND b.BoekRegelPeriode BETWEEN 201000 AND 201013
GROUP BY c.omschrijving;