Select record from table with not in other table - mysql

I am stuck in query I have a table like this
budget_details
id budget_id expenditure_head_id budget
1 1 1 1233
2 1 2 333
3 1 3 567
4 2 1 343
5 2 2 343
6 2 3 6767
7 2 4 557
expenditure_heads
id name
1 abc
2 xyz
3 qwe
4 uvw
I want to get all the expenditure_heads from budget_details that even
if not in budget_details like here budget_id=1 does not contain expenditure_head_id 4
but I want to select that to with 0 or null displaying
I tried this but it not displaying expenditure_head_id 4
select `expenditure_heads`.`name`, `budget_details`.`budget`, `budget_details`.`id` from
`budget_details`
right join `expenditure_heads` on `budget_details`.`expenditure_head_id` = `expenditure_heads`.`id`
where `budget_details`.`budget_id` = 1"

The where avoid you to get the missing row you need. The left join is done on the ON statement, so this query should work for you:
SELECT EH.name, BD.budget, BD.id FROM expenditure_heads EH
LEFT JOIN budget_details BD
ON (BD.expenditure_head_id = EH.id AND BD.budget_id = 1)

Related

query to get distinct data from child table

please provide query to getting my result.
i have two tables as follows.
price_band
id club_id name price
1 6 test 2.3
2 6 test1 3.3
price_band_seat
id price_band_id row seat block_id
1 1 a 1 1
2 1 a 2 1
3 1 b 1 2
4 2 b 2 2
and result that i want
Price block_id price_band_id row
2.3 1 1 a
2.3 2 1 b
3.3 2 2 b
query exclude that raw which block_id and price_band_id are same . in where clues you have to take club_id=6
Please try this.
SELECT
DISTINCT
A.Price,B.block_id,B.price_band_id,B.row
FROM
price_band A
INNER JOIN price_band_seat B
ON A.id = B.price_band_id
WHERE A.club_id = 6

join tables using array saved in database to be used to match tables

I need to join three tables where I got this data from database 1-1|2-0|3-2|4-0. When you break this down it will be come 1-1, 2-0, 3-2, 4-0, where the first number is "addon_id" and second number is "option_id" respectively separated by dash. This is the data I was told to work on to.
1-1, 2-0, 3-2, 4-0
paired
addon_id - option_id
1 - 1
so on...
addon_id = (1,2,3,4)
option_id = (1,0,2,0)
table photo_addons
addon_id addon_name addon_price
1 test1 123.00
2 test2 456.00
3 test3 125.00
4 test4 112.00
table photo_options
option_id option_name option_price
1 opt1 50.00
2 opt2 40.00
3 opt3 35.00
In this next table it is where you will find what options are in each addons:
table option_items
id addon_id option_id
1 1 1
2 1 2
3 2 1
4 3 1
5 3 2
6 3 3
Result should be like this:
addon_name addon_price option_name option_price
test1 123.00 opt1 50.00
test2 456.00 0.00
test3 125.00 opt2 40.00
test4 112.00 0.00
Here what I tried so far
SELECT pa.addon_name, pa.addon_price, po.option_name, po.option_price
FROM photo_addons as pa
LEFT OUTER JOIN option_items as oi
ON (oi.addon_id = pa.addon_id AND oi.option_id IN (1,0,2,0))
LEFT JOIN photo_options as po
ON po.option_id = oi.option_id
WHERE pa.addon_id IN (1,2,3,4)
but with this, it is displaying the option_id that shouldnt be displayed

Pivot query is showing wrong results

I have 2 tables, survey_product and survey_result table
survey_product looks like this:
id product
1 abc
2 asd
3 xyz
survey_result looks like this:
id user_id product_id answer
1 2 1 1
2 2 2 0
3 2 3 1
4 3 1 1
5 3 2 0
6 3 3 1
I need the result to be:
id user abc asd xyz .......
1 2 1 0 1
2 3 1 0 1
There is no easy way to do this in MySQL. You have to put case statement and convert them in to columns for all the users available.
select s.id,
case when s.user = 1 then answer else 0 end as 'abc',
...
...
from survey_result s
join user u on s.user = u.id
group by s.id
However you can simply export the result set as csv and do the necessary pivoting in an spreadsheet software like excel.

Select min/max from multiple items

I'll try to explain it as simple as possible:
First some database structure with dummy data.
Structure
tb_spec_fk
feature value
-----------------
1 1
1 2
1 3
1 4
1 5
2 2
2 3
3 1
3 4
4 2
4 3
4 4
5 1
5 3
5 5
6 3
6 5
tb_spec_feature
feature_id filter
------------------
1 2
2 2
3 2
4 2
5 1
6 0
tb_spec_value
value_id name
----------------
1 10
2 20
3 30
4 40
5 50
Now, what I want is the follow result
Result
feature_id min_value max_value
---------------------------------
1 10 50
2 20 30
3 10 40
4 20 40
But how?
Logic
Get from the tb_spec_feature where "filter" equals 2 the highest and lowest values which are present in the tb_spec_value table and connected together trough the tb_spec_fk table.
My attemps
A lot! But I'll spare you :)
SELECT
f.feature_id AS feature_id,
MAX(value.name) AS max_value,
MIN(value.name) AS min_value
FROM tb_spec_feature AS f
JOIN tb_spec_fk AS fk ON f.feature_id=fk.feature
JOIN tb_spec_value AS value ON fk.value=value.id
WHERE f.filter=2
GROUP BY f.feature_id
The two JOIN statements "link" the a feature to a value. GROUP BY groups all rows with the same feature id, and then you can take the min or max or any other aggregate function on those columns.
Demo
Here is how you can do it
select
tsf.feature_id,
tsvl.name as Min_Value,
tsvr.name as Max_Value
from tb_spec_feature as tsf
inner join (select feature , MIN(value) MinV,MAX(value)MaxV from tb_spec_fk group by feature order by feature)as tsfkl on tsfkl.feature = tsf.feature_id
left join tb_spec_value as tsvl on tsvl.value_id = tsfkl.MinV
left join tb_spec_value as tsvr on tsvr.value_id = tsfkl.MaxV
where tsf.filter = 2
group by tsf.feature_id
Output
feature_id | Min_Value | Max_Value
---------------------------------
1 | 10 | 50
2 | 20 | 30
3 | 10 | 40
4 | 20 | 40
Fiddle Demo

Mysql left join or much simpler way?

I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.