OR opertor ignores AND operator MYSQL - mysql

see the query below i want all those records where status is either using or offering and userid is not equals to 1 and coursecode is cs3333
SELECT c.`id` AS courseid,
c.`userid` AS userid,
c.`coursecode`,
m.`title`,
s.`sem_name`,
p.`professor`,
st.`status`,
a.`author_name`,
q.`quality`,
m.`comments`,
m.`price`,
m.`material`
FROM sc_courses c
JOIN sc_c_materials m
ON c.`id`=m.`courseid`
JOIN sc_semesters s
ON s.`id`=c.`semid`
JOIN sc_professors p
ON c.`profid`=p.`id`
JOIN sc_status st
ON c.`statusid`=st.`id`
LEFT JOIN sc_authors a
ON m.`authorid`=a.`id`
JOIN sc_quality q
ON m.`qualityid`=q.`id`
WHERE st.`status` = "offering" OR st.`status` = "using" AND c.`userid` != "1" AND c.`coursecode` = "CS3333";
the query is running but it ignores both the and operators the above query return all records where userid is 1 and coursecode is not equal to cs3333 but i dont want these records plz tell me what am i doing wrong????

You just need brackets
WHERE (st.`status` = "offering" OR st.`status` = "using") AND c.`userid` != "1" AND c.`coursecode` = "CS3333";

That is because priority. You should place ( .. ) to change order of comparison.
(st.`status` = "offering" OR st.`status` = "using")
AND c.`userid` != "1"
AND c.`coursecode` = "CS3333"

opss stupid mistake here it is working with this query
SELECT c.`id` AS courseid,
c.`userid` AS userid,
c.`coursecode`,
m.`title`,
s.`sem_name`,
p.`professor`,
st.`status`,
a.`author_name`,
q.`quality`,
m.`comments`,
m.`price`,
m.`material`
FROM sc_courses c
JOIN sc_c_materials m
ON c.`id`=m.`courseid`
JOIN sc_semesters s
ON s.`id`=c.`semid`
JOIN sc_professors p
ON c.`profid`=p.`id`
JOIN sc_status st
ON c.`statusid`=st.`id`
LEFT JOIN sc_authors a
ON m.`authorid`=a.`id`
JOIN sc_quality q
ON m.`qualityid`=q.`id`
WHERE c.`userid` != "1" AND c.`coursecode` = "CS3333" AND st.`status` = "offering" OR
c.`userid` != "1" AND c.`coursecode` = "CS3333" AND st.`status` = "using";

Related

Simplified SQL Query

Currently we have to run the following SQL Query to find all customers that have not been assigned access to a downloadable product.
SELECT c.entity_id as customer_id, dp.purchased_id as purchased_id
FROM magento_dev.sales_order as so
JOIN magento_dev.sales_order_address as soa on so.entity_id = soa.parent_id
JOIN magento_dev.customer_entity as c on c.email = soa.shipping_email
JOIN magento_dev.downloadable_link_purchased as dp on so.increment_id = dp.order_increment_id
where
so.customer_id = 12345 and
so.created_at > '2021-04-01' and
soa.shipping_email is not null and
dp.customer_id != c.entity_id
;
Which produces this:
customer_id purchased_id
99999 55555
Then with the results, we manually have to go through, and for each customer_id, and purchase_id run the following query:
UPDATE `magento_dev`.`downloadable_link_purchased` SET `customer_id` = '99999' WHERE (`purchased_id` = '55555');
So for the SQL Ninja's, any suggestions on how to make this a single query that finds the mismatched assignments and then does the update/insert at once?
Just put all the JOINs in the UPDATE query.
UPDATE `magento_dev`.`downloadable_link_purchased` AS dp1
JOIN magento_dev.sales_order as so
JOIN magento_dev.sales_order_address as soa on so.entity_id = soa.parent_id
JOIN magento_dev.customer_entity as c on c.email = soa.shipping_email
JOIN magento_dev.downloadable_link_purchased as dp
ON so.increment_id = dp.order_increment_id
AND dp.customer_id != c.entity_id
AND dp1.purchased_id = dp.purchased_id
SET dp1.customer_id = c.entity_id
where
so.customer_id = 12345 and
so.created_at > '2021-04-01' and
soa.shipping_email is not null
Can be done like sub query and join with the table which should be updated,
UPDATE
magento_dev.downloadable_link_purchased F
JOIN
(SELECT c.entity_id as customer_id, dp.purchased_id as purchased_id
FROM magento_dev.sales_order as so
JOIN magento_dev.sales_order_address as soa on so.entity_id = soa.parent_id
JOIN magento_dev.customer_entity as c on c.email = soa.shipping_email
JOIN magento_dev.downloadable_link_purchased as dp on so.increment_id = dp.order_increment_id
WHERE
so.customer_id = 12345 and
so.created_at > '2021-04-01' and
soa.shipping_email is not null and
dp.customer_id != c.entity_id) S
ON F.purchased_id = S.purchased_id
SET F.customer_id = S.customer_id;

mysql query not working with where in condition

I am running following query in mysql.
SELECT jobtype_has_trade.jobtype_id,users_jobtype.user_id
FROM users
LEFT JOIN subcontractor ON users.subcontractor_id = subcontractor.id
AND subcontractor.quotations = "YES"
LEFT JOIN users_jobtype ON users.id = users_jobtype.user_id
AND users_jobtype.status = "A"
LEFT JOIN jobtype_has_trade ON users_jobtype.jobtype_trade_id = jobtype_has_trade.id
WHERE users.is_subcontractor = "YES"
AND users.has_android = "YES"
AND jobtype_has_trade.jobtype_id IN (1,3,4)
ORDER by users_jobtype.user_id ASC
and i am getting this output
In above records i need only that user_id that have jobtype_id 1,3 and 4. so, user_id 3 and 7 is acceptable and 9 is not because it has only 3 and 4 as jobtype_id... in sort how can i get only 3 and 7 as user_id in above query?
one option could be to use having clause with group by
SELECT users_jobtype.user_id
FROM users
LEFT JOIN subcontractor ON users.subcontractor_id = subcontractor.id
AND subcontractor.quotations = "YES"
LEFT JOIN users_jobtype ON users.id = users_jobtype.user_id
AND users_jobtype.status = "A"
LEFT JOIN jobtype_has_trade ON users_jobtype.jobtype_trade_id = jobtype_has_trade.id
WHERE users.is_subcontractor = "YES"
AND users.has_android = "YES"
AND jobtype_has_trade.jobtype_id IN (1,3,4)
group by users_jobtype.user_id
having count(distinct jobtype_has_trade.jobtype_id)=3
just check existing of jobtype_id in 1,3,4 for each user. I don't know about your schema
if you have jobtype_id in users_jobtype then you don't need to join to jobtype_has_trade in subquery in where (in my solution)
SELECT jobtype_has_trade.jobtype_id,
users_jobtype.user_id
FROM users
LEFT JOIN subcontractor ON users.subcontractor_id = subcontractor.id
AND subcontractor.quotations = "YES"
LEFT JOIN users_jobtype ON users.id = users_jobtype.user_id
AND users_jobtype.status = "A"
LEFT JOIN jobtype_has_trade ON users_jobtype.jobtype_trade_id = jobtype_has_trade.id
WHERE users.is_subcontractor = "YES"
AND users.has_android = "YES"
AND jobtype_has_trade.jobtype_id IN (1,
3,
4)
and exists( select jobtype_id from jobtype_has_trade inner join users_jobtype on users_jobtype.jobtype_trade_id=jobtype_has_trade.id where users_jobtype.user_id=users.id and
jobtype_has_trade.jobtype_id=1
)
and exists( select jobtype_id from jobtype_has_trade inner join users_jobtype on users_jobtype.jobtype_trade_id=jobtype_has_trade.id where users_jobtype.user_id=users.id and
jobtype_has_trade.jobtype_id=3
)
and exists( select jobtype_id from jobtype_has_trade inner join users_jobtype on users_jobtype.jobtype_trade_id=jobtype_has_trade.id where users_jobtype.user_id=users.id and
jobtype_has_trade.jobtype_id=4
)
ORDER BY users_jobtype.user_id ASC

Suggestions on how I can optimize this MySQL query to run faster

SELECT *
FROM members
WHERE memberid IN (SELECT follows.followingid
FROM follows
WHERE follows.memberid = '$memberid'
AND follows.followingid NOT IN (SELECT memberid
FROM userblock))
AND memberid NOT IN (SELECT blockmemberid
FROM userblock
WHERE memberid = '$memberid')
The query above is taking nearly 4 seconds to execute in MySQL and I want to know if anyone has any suggestions on how I might improve/optimize it to achieve a faster execution time?
Replace the in clauses with joins. I think the following captures the logic. Note that the not in turns into a left join with a condition in the where clause finding a non-match
SELECT m.*
FROM members m
follow f
on m.memberid = f.followingid and
f.memberid = $memberid left join
userblock ubf
on follows.followingid = ubf.memberid left join
userblock ub
on m.memberid = ub.blockmemberid and
ub.memberid = '$memberid'
where ub.blockmemberid is null and
ubf.memberid is null;
This looks similar but you have less nested queries.
SELECT *
FROM members m
WHERE EXIST (SELECT f.followingid
FROM follows f
WHERE f.memberid = '$memberid'
AND f.followingid = m.memberid)
AND NOT EXIST (SELECT u.blockmemberid
FROM userblock u
WHERE (m.memberid = '$memberid'
AND u.blockmemberid = m.memberid)
OR
(u.blockmemberid = m.memberid
AND u.memberid = m.memberid) )
This is the logic I reversed-engineered from your code without seeing tables.
SELECT m.*
FROM members m
INNER JOIN follows f ON f.followingid = m.memberid AND
f.memberid = '$memberid'
LEFT OUTER JOIN userblock ub1 ON f.followingid = ub1.memberid
LEFT OUTER JOIN userblock ub2 ON m.memberid = ub2.blockmemberid AND
ub2.memberid = '$memberid'
WHERE ub1.memberid IS NULL AND ub2.blockmemberid IS NULL

MySQL not recognizing virtual column / field

I'm trying to manipulate two virtual fields generated of a subquery into a new field, but MySQL is telling me that "GP" is an unknown column, but it has been already declared. Please, take a look at my query:
SELECT *,
(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe = e.id_equipe AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GP',
(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe != e.id_equipe AND (jg.fk_id_equipe1 = e.id_equipe OR jg.fk_id_equipe2 = e.id_equipe) AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GC',
(SELECT COUNT(id_wo) as WOs FROM tb_wo as w INNER JOIN tb_jogo as j ON (w.fk_id_jogo = j.id_jogo) WHERE w.fk_id_equipe = e.id_equipe AND j.fk_id_campeonato = g.fk_id_campeonato) as 'WO',
(GP+(GC*-1)) as 'SALDO'
FROM tb_equipe as e
INNER JOIN tb_gruposEquipes as ge ON (e.id_equipe = ge.fk_id_equipe)
INNER JOIN tb_grupos as g ON (g.id_grupo = ge.fk_id_grupo)
WHERE g.fk_id_campeonato = 23
ORDER BY WO ASC
As you can see, "SALDO" would be the result of "GP-GC". But MySQL do not recognize these columns
How can I solve this?
SOLUTION
Thanks a lot for the help, guys, but I figured out the problem.
"SALDO" couldn't be created by substracting GC of GP because "GP" and "GC" doesn't exist during runtime.
So, when you need to manipulate virtual fields during the runtime, you'll have to repeat the code that generated the virtual field.
Then, if you have this select:
SELECT *, (field1+1) as 'GP', (field2+1) as 'GC', (GP+GC) as 'SALDO' FROM (...)
You'll need to replace it with this:
SELECT *, (field1+1) as 'GP', (field2+1) as 'GC', ((field1+1)+(field2+1)) as 'SALDO' FROM (...)
Push the stuff into in inner query
SELECT core.*,
(GP+(GC*-1)) as 'SALDO'
from (
(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe = e.id_equipe AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GP',
(SELECT COUNT(id_gol) FROM tb_gol as gol
INNER JOIN tb_jogo as jg ON(gol.fk_id_jogo = jg.id_jogo)
WHERE gol.ic_excluido != '*' AND gol.fk_id_equipe != e.id_equipe AND (jg.fk_id_equipe1 = e.id_equipe OR jg.fk_id_equipe2 = e.id_equipe) AND jg.fk_id_campeonato = g.fk_id_campeonato) as 'GC',
(SELECT COUNT(id_wo) as WOs FROM tb_wo as w INNER JOIN tb_jogo as j ON (w.fk_id_jogo = j.id_jogo) WHERE w.fk_id_equipe = e.id_equipe AND j.fk_id_campeonato = g.fk_id_campeonato) as 'WO' )
FROM tb_equipe as e
) as core
INNER JOIN tb_gruposEquipes as ge ON (core.id_equipe = ge.fk_id_equipe)
INNER JOIN tb_grupos as g ON (g.id_grupo = ge.fk_id_grupo)
WHERE g.fk_id_campeonato = 23
ORDER BY WO ASC

Converter sql query to Linq

I need to convert this sql query to linq to sql and the result returns a IEnumerable:
select VisualAidName, v.VisualAidID, vs.VisualAidStatusName,
br.BrandName, v.IsEnabled, v.VisualAidCode, v.DateApproved,
br.BrandID, type, UserFirstName+ ' ' + UserLastName as name, AreaID
from VisualAids v inner join VisualAidStatus vs
on v.VisualAidStatusId = vs.VisualAidStatusId
inner join brands br
on v.BrandID = br.BrandId
inner join VisualAids_Areas_Link vareas
on v.VisualAidID = vareas.VisualAidID
left join users us
on v.Owner = us.UserID
where
AreaID IN (
select areaid
from Users inner join Users_Area_Link
on Users.UserID = Users_Area_Link.UserID
where Users.UserID= 3
)
I did this:
IEnumerable<Visual_Aid> visualAll = from v in Context.VisualAids
join vs in Context.VisualAidStatus on v.VisualAidStatusId equals vs.VisualAidStatusId
join br in Context.Brands on v.BrandID equals br.BrandId
join us in Context.Users on v.Owner equals us.UserID into vadis
from x in vadis.DefaultIfEmpty()
select new Visual_Aid()
{
VisualAid_Name = v.VisualAidName,
VisualAid_Id = v.VisualAidID,
VisualAid_StatusName = vs.VisualAidStatusName,
VisualAid_BrandsName = br.BrandName,
VisualAid_IsEnabled = bool.Parse(v.IsEnabled.ToString()),
VisualAid_Code = v.VisualAidCode,
VisualAid_DateApp = v.DateApproved.ToString() ?? "",
VisualAid_BrandId = int.Parse(v.BrandID.ToString()),
VisualAid_Type = v.Type,
VisualAid_Owner = x.UserID == null ? "" : x.UserFirstName + " " + x.UserLastName
};
but I need to do the part of the subquery, ie, I need to include this:
where AreaID IN (
select areaid from Users inner join Users_Area_Link
on Users.UserID = Users_Area_Link.UserID where Users.UserID= 3
)
Anybody know how? thank you very much in advance
You can add this as a where statement:
.......
join us in Context.Users on v.Owner equals us.UserID into vadis
from x in vadis.DefaultIfEmpty()
where (
from user in Context.Users
join userArea in Users_Area_Link
on user.UserID equals userArea.UserID
where user.UserID==3
select userArea.areaid
).Contains(????.AreaID)
select new Visual_Aid()
{
.......