Pardon for being naive. This is a simple simple question but i am unable to figure out how to query this
Query :
I have 2 tables
kknagar_data & kknagar_rates
I am trying to query out in such a way that
- get current record from kknagar_data - i was able to achieve this
- get kk_egg,kk_cash of previous record from current in kknagar_data
- get rates from kknagar_rates by joining kk_rate_id in kk_nagar_rates & kk_nagar_data - I was able to achieve this
What i am trying to achieve
I want all the above 3 points in a single query.
I was able to do point 1 & 3 and i queried like this
SELECT kk_data_id,kk_date,kk_egg,kk_cash,kk_in,kk_out,kk_expenditure,..... from kknagar_data INNER JOIN kknagar_rates on kknagar_data.kk_rate_id = kknagar_rates.kk_rate_id"
but i am unable add point 2 to the above query so that i get all the data in a single shot
What i tried :
I tried with Unions but i get error because the columns doesnt match
What i am using now :
Currently i use 2 different queries to achieve the above
SELECT kk_data_id,kk_date,kk_egg,kk_cash,kk_in,kk_out,kk_expenditure,..... from kknagar_data INNER JOIN kknagar_rates on kknagar_data.kk_rate_id = kknagar_rates.kk_rate_id"
and
Select kk_egg,kk_cash FROM kknagar_data WHERE kk_data_id < 3 ORDER BY kk_data_id DESC LIMIT 1
It gives me the result but i am trying to change this to make it more efficient. Another reason is when i json out the values, i find it difficult to decode multiple objects.
Goal :
Get current record with 2 fields of previous record and join with kk_nagar_rates & kk_nagar_data using rate_id
Any guindance will be much helpful.
Thanks in Advance