Obtain value from same column twice with different condition in single query - mysql

Hii I want to Obtain value from same column twice with different condition in single query
Here is my code
SELECT
o.products_options_name,
ov.products_options_values_name AS products_options_values_name,
a.options_values_quantity
FROM
table_products_options o,
table_products_attributes a
WHERE
a.options_id = o.products_options_id
AND
o.products_options_values_id = a.options_values_id
AND
a.products_id = :products_id
AND
a.options2_values_id = options2_values_id
i want to obtain value of column products_options_values_name again with different condition like where a.options2_values_id = ov.products_options_values_id in same query. How can i do this?

try by using UNION operator,
SELECT o.products_options_name, ov.products_options_values_name AS products_options_values_name,
a.options_values_quantity
FROM
table_products_options o, table_products_attributes a
WHERE
a.options_id = o.products_options_id AND ov.products_options_values_id = a.options_values_id
AND a.products_id = :products_id1 AND
a.options2_values_id = options2_values_id
UNION
SELECT o.products_options_name, ov.products_options_values_name AS products_options_values_name,
a.options_values_quantity
FROM
table_products_options o, table_products_attributes a
WHERE
a.options_id = o.products_options_id AND ov.products_options_values_id = a.options_values_id
AND a.products_id = :products_id2 AND
a.options2_values_id = options2_values_id

Union / Union all would be best
like
select o.products_options_name, ov.products_options_values_name as
products_options_values_name, a.options_values_quantity
from
table_products_options o, table_products_attributes a
where
a.options_id = o.products_options_id and ov.products_options_values_id =
a.options_values_id and a.products_id = :products_id and a.options2_values_id =
options2_values_id
UNION
select o.products_options_name, ov.products_options_values_name as products_options_values_name, a.options_values_quantity
from
table_products_options o, table_products_attributes a
where Diffrent Clause
Union Works like Distinct, Union ALL will return duplicate values if your clauses both return the value.
Alternatively use OR in your where clause and (a.b = c.d OR c.d=b.a) and

You can specify the condition that you want in the on clause. on clause? Your query should use explicit join syntax.
Here is one way to express the query that you want:
select o.products_options_name, ov.products_options_values_name as products_options_values_name,
a.options_values_quantity
from table_products_options o join
table_products_attributes a
on a.options_id = o.products_options_id and
(ov.products_options_values_id = a.options_values_id or
a.options2_values_id = options2_values_id
)
where a.products_id = :products_id;

Related

Parentheses in mysql query giving different results

I have a query that is giving me different results when i enclosed it in Parentheses, however when I run it without Parentheses its giving me different results. I want to apply Union in between so I have to use Parentheses as without union doesn't work.
The query is as follows:
SELECT Distinct
recurring_billing.id,
recurring_billing.kid_id,
recurring_billing.class_id,
recurring_billing.app_id,
recurring_billing.Region_ID,
CC.month_name,
CC.billing_year,
CASE
WHEN
CC.month_name = recurring_billing.billing_month
AND CC.billing_year = recurring_billing.billing_year
THEN
recurring_billing.billing_status
ELSE
'Pending'
END
AS billing_status, tblkids.kid_name, tblkids.kid_Lastname, tblkids.kid_EMail, tbl_app.app_CCExp AS cc_exp, tbl_app.app_CCName AS cc_name, tbl_app.app_CCNumber AS cc_number, tbl_app.app_CCType AS cc_type, tblclasses.cla_EndDate, tblclassdays.classday_day, CC.remainingclasses, tbl_app.cost_per_class, CC.remainingclasses * tbl_app.cost_per_class AS cost_amount,
CASE
WHEN
OP.override_amt IS NOT NULL
THEN
OP.override_amt
ELSE
CC.remainingclasses * tbl_app.cost_per_class
END
AS pmt_amount,
CASE
WHEN
OP.process_payment = False
THEN
OP.process_payment
ELSE
True
END
AS process_payment
FROM
recurring_billing
LEFT JOIN
tblkidsxclass
ON recurring_billing.kid_id = tblkidsxclass.kxc_kidid
AND recurring_billing.app_id = tblkidsxclass.kxc_appid
LEFT JOIN
tbl_app
ON recurring_billing.app_ID = tbl_app.app_ID
LEFT JOIN
tblkids
ON recurring_billing.kid_id = tblkids.kid_ID
LEFT JOIN
tblclasses
ON recurring_billing.class_id = tblclasses.cla_ID
LEFT JOIN
tblclassdays
ON tblclasses.cla_ID = tblclassdays.classday_classID
INNER JOIN
(
SELECT
MONTHNAME(classday_day) AS month_name,
YEAR(classday_day) AS billing_year,
cla_ID,
COUNT(classday_classid) AS remainingclasses,
c.cost_per_class AS cost_per_class,
COUNT(classday_classid) * cost_per_class AS TotalClassCost
FROM
tblclassdays
JOIN
(
SELECT
tblclasses.cla_ID,
tblclasses.cost_per_class,
tblclasses.cla_nextclass AS next1,
tblclasses_1.cla_nextclass AS next2
FROM
tblclasses
LEFT JOIN
tblclasses AS tblclasses_1
ON tblclasses.cla_nextclass = tblclasses_1.cla_ID
WHERE
tblclasses.cla_ID IN
(
SELECT DISTINCT
recurring_billing.class_id
FROM
recurring_billing
LEFT JOIN
tblkidsxclass
ON recurring_billing.kid_id = tblkidsxclass.kxc_kidid
AND recurring_billing.app_id = tblkidsxclass.kxc_appid -- LEFT JOIN tblkids ON recurring_billing.kid_id = tblkids.kid_ID
LEFT JOIN
tblclasses
ON recurring_billing.class_id = tblclasses.cla_ID -- LEFT JOIN tblclassdays ON tblclasses.cla_ID = tblclassdays.classday_classID
WHERE
ISNULL(tblkidsxclass.kxc_dropoutdate)
AND tblkidsxclass.pmt_option = 'Recurring'
AND tblclasses.cla_active = TRUE -- AND tblclasses.cla_EndDate >= NOW()
GROUP BY
recurring_billing.id
)
)
c
ON tblclassdays.classday_classid IN
(
c.cla_ID,
c.next1,
c.next2
)
WHERE
tblclassdays.classday_noclass = FALSE
AND MONTH(classday_day) = 11
AND YEAR(classday_day) = 2020
AND
(
CONCAT(CAST(classday_day AS DATE), ' ', CAST(classday_endtime AS TIME)) > CAST(CURDATE() AS DATETIME)
)
GROUP BY
cla_ID
)
CC
ON CC.cla_ID = tblclassdays.classday_classID
LEFT JOIN
override_payments OP
ON recurring_billing.app_id = OP.app_id
AND recurring_billing.kid_id = OP.kid_id
AND CC.month_name = OP.billing_month
AND CC.billing_year = OP.billing_year
WHERE
ISNULL(tblkidsxclass.kxc_dropoutdate)
AND tblkidsxclass.pmt_option = 'Recurring'
AND tblclasses.cla_active = TRUE
AND recurring_billing.kid_id NOT IN
(
SELECT
kid_id
from
recurring_billing
where
billing_month = 'November'
and billing_year = '2020'
)
-- AND tblclasses.cla_EndDate >= NOW()
GROUP BY
recurring_billing.id
ORDER BY
recurring_billing.id ASC
Same query if enclosed in Parentheses will give me different results.
Parentheses are not required for a UNION statement. The following 2 union statements both work and yield the same results.
SELECT 1 UNION SELECT 2;
(SELECT 1) UNION (SELECT 2);

MySQL: From sub query to a single query

I have this query which i believe can be optimized:
SELECT floors.id, floors.floor FROM floors
WHERE floors.societies_id = 1
AND floors.status = 'Y'
AND floors.id NOT IN (
SELECT DISTINCT(floors.id) FROM floors
INNER JOIN societies ON societies.id = floors.societies_id
INNER JOIN resident_floors ON resident_floors.floors_id = floors.id
WHERE societies.id = 1
AND floors.status = 'Y'
)
Is this query fine to use or there it can be improved..?
It looks like you want to get all floors that aren't present in resident_floors. For this we can left join RF in and ask for only rows where the join failed resulting in a null in RF:
SELECT floors.* FROM floors
INNER JOIN societies ON societies.id = floors.societies_id
LEFT JOIN resident_floors ON resident_floors.floors_id = floors.id
WHERE societies.id = 1
AND floors.status = 'Y'
AND resident_floors.floors_id IS NULL

How to select distinct max value from multiple join table MySQL

This is my current query for selecting data:
select a.No_Registrasi, a.Nama_CTKI, b.Nama_Negara, c.ID_Rad, d.ID_Lab
FROM tb_registrasi a
JOIN tb_negara_tujuan b
ON a.ID_Negara = b.ID_Negara
JOIN tb_radiologi c
ON a.No_Registrasi = c.No_Registrasi
JOIN tb_laboratorium d
ON a.No_Registrasi = d.No_Registrasi
And here's the result:
How do i distinct those ID_Rad and ID_Lab and I need the max value from each ID_Rad and ID_Lab so it will be like this:
Use GROUP BY and MAX, try this;)
select a.No_Registrasi, a.Nama_CTKI, b.Nama_Negara, CONCAT('RA-', MAX(substring(c.ID_Rad, 4) + 0)) AS ID_Rad, CONCAT('Lab-',MAX(substring(d.ID_Lab, 5) + 0)) AS Id_Lab
FROM tb_registrasi a
JOIN tb_negara_tujuan b
ON a.ID_Negara = b.ID_Negara
JOIN tb_radiologi c
ON a.No_Registrasi = c.No_Registrasi
JOIN tb_laboratorium d
ON a.No_Registrasi = d.No_Registrasi
GROUP BY a.No_Registrasi, a.Nama_CTKI, b.Nama_Negara

DISTINCT keyword not working in mysql query

I have ran into a problem that my mysql query is not working as expected. I am joining 3 tables using union and from the result i want to ignore the repeating words. For that I have used DISTINCT keyword. But it is not working as expected.
My query is,
SELECT DISTINCT(catnam), sub2id
FROM tbl_first_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
UNION
SELECT DISTINCT(catnam), sub2id
FROM tbl_third_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
UNION
SELECT DISTINCT(catnam), sub2id
FROM tbl_fourth_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
And my result is,
catnam sub2id
------ ------
Levi's 4
United 1
Reebok 130
Jack 18
Proline 77
Levi's 161
Arrow 284
In the above result Levi's is repeating. How to ignore this. I need only one Levi's. Is there any way to avoid this. I am stuck in here.
MySQL DISTINCT with multiple columns
You can use the DISTINCT clause with more than one column. In this case, MySQL uses the combination of all columns to determine the uniqueness of the row in the result set.
http://www.mysqltutorial.org/mysql-distinct.aspx
if you want solve your problem, you can use group_concat() function then you group by catnam for all union queries :
SELECT name ,GROUP_CONCAT(id)
FROM your_table
GROUP BY name ;
so your query can be:
SELECT distinct(catnam) as catnam, GROUP_CONCAT(sub2id )
FROM tbl_first_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
UNION
SELECT distinct(catnam) as catnam, GROUP_CONCAT(sub2id )
FROM tbl_third_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
UNION
SELECT distinct(catnam) as catnam, GROUP_CONCAT(sub2id )
FROM tbl_fourth_page_products f INNER JOIN subcategory_level2 s
ON f.brand_id = s.sub2id
WHERE f.title_cat = 'men'
group by catnam
I'd try:
SELECT n, GROUP_CONCAT(sub2id )
FROM
(
SELECT catnam n, sub2id
...
UNION
SELECT catnam, sub2id
...
)
GROUP BY n

SELECT INNER JOIN only returns one result

I'm having trouble selecting some data from 2 tables in my database.
The tables are psthostess and psttodo-uit.
In my psthostess I want to select the fields Code and Name.
In my psttodo-uit I want to select:
sum of PB=1
sum of PG=1
sum of PA=1
sum of h.GoedkeuringDoorNew=GF
sum of h.GoedkeuringDoorNew=SB
sum of h.GoedkeuringDoorNew=VIA
sum of h.Blanco
This is my query:
SELECT p.Code, p.Name, sum(h.PB = 1), sum(h.PG = 1), sum(h.PA = 1),
sum(h.GoedkeuringDoorNew = 'GF'), sum(h.GoedkeuringDoorNew = 'SB'),
sum(h.GoedkeuringDoorNew = 'VIA'), sum(h.Blanco)
FROM psthostess p
INNER JOIN `psttodo-uit` h ON h.`Hostess Code` = p.Code
WHERE p.Indienst = 1
The problem is that I always get a result of one row. But there are multiple rows in psthostess with Indienst = 1 (WHERE). How can I fix this?
You need to group it by non-aggregated columns.
SELECT p.Code, p.Name, sum(h.PB = 1),
sum(h.PG = 1), sum(h.PA = 1), sum(h.GoedkeuringDoorNew = 'GF'),
sum(h.GoedkeuringDoorNew = 'SB'), sum(h.GoedkeuringDoorNew = 'VIA'), sum(h.Blanco)
FROM psthostess p
INNER JOIN `psttodo-uit` h ON h.`Hostess Code` = p.Code
WHERE p.Indienst = 1
group by p.Code, p.Name