sql multiple where on join - mysql

A have a table called advert_property
And I have a table advert, which is not important, advert properties connects to advert by advert_id column in advert_property table.
I wrote this SQL request :
SELECT *
FROM `advert`
JOIN advert_property ON advert.id = advert_property.advert_id
WHERE (advert_property.property_id = 1
AND advert_property.property_value = "Манчего")
AND (advert_property.property_id = 2
AND advert_property.property_value = "козий")
What I want to get, is advert that have certain properties, for example : I want an idvert that have property_id = 1 and property_value = "Манчего" AND have property_id = 2 and property_value = "козий". SQL request returns null, how should I change my SQL request. Thanks!

Assuming I'm understanding your question correctly and you want to return all adverts that have both properties, then there are a couple ways of doing this using multiple joins, exists, in, group by...
Here is the method using multiple joins:
SELECT a.*
FROM `advert` a
JOIN advert_property ap ON a.id=ap.advert_id
AND ap.property_id = 1 AND ap.property_value = 'Манчего'
JOIN advert_property ap2 ON a.id=ap2.advert_id
AND ap2.property_id = 2 AND ap2.property_value = 'козий'

You can't return all columns * where the property_id is both 1 and 2 because a record can't have two values for the same field. You can, however, return records where the property_id is 1 OR 2. You could then have it only return DISTINCT advert_id where this is true:
SELECT DISTINCT advert_id
FROM `advert` JOIN advert_property ON advert.id=advert_property.advert_id
WHERE (advert_property.property_id = 1 AND advert_property.property_value = "Манчего")
OR (advert_property.property_id = 2 AND advert_property.property_value = "козий")

Query which you are trying to execute will never give you a result because it is trying to get a row with property id = 1 and property id = 2
For same row, there will never be two property_ids (1,2).
You need to review your where conditions.
My be what you need in where condition is as below(Either id will be 1 or id will be 2):
Try this :
(advert_property.property_id = 1 AND advert_property.property_value = "Манчего")
OR
(advert_property.property_id = 2 AND advert_property.property_value = "козий")

Related

How to get only all rows that apply this WHERE clause?

I have two tables, tasks and views, with the following structure:
tasks
-- id
-- status
views
-- id
-- taskid (FK of tasks.id)
-- status
And the tasks table, has a row with id = 1 and status = 1, whilst the views table has two rows with id = 1, taskid = 1, status = 1 and id = 2, taskid = 1, status = 0.
When I try to get all the tasks id that have all its views status set to 1 and the task's status itself set to 1 too and only, then I get in return a row with task id = 1 because view number 1 is set to 1 and view number 2 is set to 0.
So basically, what I need is an SQL statement that returns only one row for each task that has all its views and task status set to 1 (tasks.status = 1, views.status = 1) and only when that happens, and if any of the tasks' views is set to something different than 1, then the SQL statement doesn't return it.
Here is my SQL Statement so far which kind of works, but there is still something I am missing because it doesn't work as expected. Sorry if something isn't clear!
SELECT tasks.id FROM tasks JOIN views ON tasks.id = views.taskid WHERE tasks.status = 1 AND views.status = 1;
Join tasks to a query that uses aggregation to return only the taskids with min status set to 1 (which means there is no 0):
SELECT t.id
FROM tasks t
INNER JOIN (
SELECT taskid
FROM views
GROUP BY taskid
HAVING MIN(status) = 1
) v ON v.taskid = t.id
WHERE t.status = 1;
with t as (select taskid,
count(status) status_cnt,
sum(case when status = 1 then 1 else 0 end) as status_1_cnt
from views
group by taskid),
t2 as (select taskid from t where status_1_cnt > 0 and status_cnt = status_1_cnt)
select tasks.id from tasks join t2 on tasks.id = t2.taskid and status = 1
so If I am reading your question correctly you want all of the statuses in the view to be 1 per taskid. so I would count the view statuses and compare that count to where the view status is 1. (the case statement).
then just join this to the task table where the task status is 1
(although I like #forpas answer better)
Conceptually you only want to join on the records in the views table where status = 1, so like this:
SELECT A.id
FROM
tasks A
INNER JOIN
(
SELECT
tasks.taskid
FROM
views
WHERE
view.status = 1
) B
ON A.id = B.taskid
Although the syntax is less intuitive, this is equivalent and shorter:
SELECT
tasks.id
FROM
tasks
JOIN views
ON tasks.id = views.taskid
AND view.status = 1
WHERE
tasks.status = 1
This should also work, for a different reason (more like a trick):
SELECT A.id
FROM
tasks A
INNER JOIN views B
ON a.id = b.taskid
and a.status = b.status
WHERE
A.status = 1
This might be more stringent of a test if thats what you need (the matching records in views with the requirement that no other records with status = 0 exist in views) - but I would like to avoid this style of using a correlated subquery in real life if the tables are of an significant size:
SELECT A.id
FROM
tasks A
INNER JOIN views B
ON A.id = B.taskid
WHERE
A.status = 1
AND B.status = 1
AND NOT EXISTS (SELECT * FROM views c
WHERE c.taskid = b.taskid and c.status = 0)
Finally this is a solution that thinks conceptually more in terms of the intersection of the sets:
SELECT A.id
FROM
tasks a
INNER JOIN views a
ON A.id = b.taskid
AND b.status = 1
LEFT JOIN views c
ON a.id = c.taskid
AND c.status = 0
WHERE
A.status = 1
AND c.status is null
I just saw that forpas has just shown a different but very good solution using aggregation with a min() clause to select only the appropriate records from views for use in joining to tasks which seems like it may be the winner to me :)
If I understand you correctly, you want to get id of task , if and ONLY if it's status = 1, and there are particular records in views table with ONLY same status = 1.
Then your query would be like this:
select tasks.id
from tasks
where status =1 and not exists(
select 1
from views
where taskid=tasks.id and views.status!=tasks.status)
Check demo https://www.db-fiddle.com/f/cQfQMx5LGJN516ND2iVj8y/3

SQL compare from c1.fieldA to c2.fieldB When equal to certain value

veh_user table:
veh_model table:
I have the user Identifier: steam:110000104bd6595 corresponding to veh_user.user.
I want to select all the field that respect:
veh_user.user = steam:110000104bd6595
the result from the 1).model = veh_model.id_model WHERE type = 3
I don't know if I'm clear here, but I need the result to operate, and I want to know if there is a better solution than made 2 requests
select * from veh_user u
inner join veh_model m
on m.id_model = u.model
where u.user = 'steam:110000104bd6595'
and m.type = 3
Here's an alternate solution that avoids the use of join:
select * from veh_user vu
where vu.user = 'steam:110000104bd6595'
and exists(select 1 from veh_model
where type = 3 and model_id = vu.model)

Count if a user has reached the borrwing limit

I've setup a fiddle with tables and data here
I'm trying to write a single sql to check if user has reached the borrowing limit for each category.
Right now, it's done using severals sql statements called after each other.
But the way it goes is simple.
memId and id come through a querystring.
$medId = $_POST['memId']; Using 1 for this example. This is the members Id.
$id = $_POST['id']; Using 4 for this example. This is the item being lent.
After that I do:
select id, holder from collection_db where id = 4 // We have a valid item
select borrowMax from collection_db where id = (holder from the previous select) and category = 10 //Result = 2. Category indicates its a label and not a borrowable item.
select count(borrowedId) from lendings where memId = 1 and holder = (holder from the 1st query) //He's borrowed 2, under 1, so cant borrow any more. User 2 may borrow however.
if (count => borrowMax) {echo 'Cannot borrow more.';} else {echo 'Added to'}
How can this be combined into a single sql or is it best left this way?
This seems to produce a correct result set:
SELECT col1.id, col1.holder, col2.borrowMax, count(lend.borrowedId) as `count`
FROM collection_db col1
INNER JOIN collection_db col2
ON col1.holder = col2.id
INNER JOIN lendings lend
ON col1.holder = lend.holder
WHERE col1.id = $id
AND col2.category = 10
AND lend.memId = $medId
I think this combines the queries:
select max(c.borrowMax) as BorrowMax, COUNT(*)
from collection_db c join
collection_db c1
on c.id = c1.holder and c1.id = 4 and c.category = 10 join
lendings l
on l.holder = c1.holder;
It does make an assumption that the join between c and c1 does not produce duplicate rows. But you have this requirement by using = in the original query (rather than join).

JOIN two tables while grouping by an index and maching for multiple foreign ids

I have the 3 Following Tables:
objects:
ObjectID ObjectDescription
1 "first"
2 "second"
attributes:
AttributeID AttributeDescription
1 "att1"
2 "att2"
3 "att3"
4 "att4"
attributelink:
AttributeID ObjectID
1 1
2 1
4 1
Now my question: I want now have some attributes selected and want to know, which Object has all my selected Attributes. I've tried the following:
SELECT * FROM `objects`
INNER JOIN `attributelink`
ON `objects`.`ObjectID` = `attributelink`.`ObjectID`
WHERE `attributelink`.`AttributeID` =1 AND `attributelink`.`AttributeID` =2
GROUP BY `objects`.`ObjectID`
That obviously doesn't work, because one row can't have 2 AttributeIDs, but how can I archieve this?
You have to join on the attributelink table once for each selected attribute you want to check for:
SELECT o.ObjectID
FROM objects o
INNER JOIN attributelink a1 ON o.ObjectID = a1.ObjectID AND a1.AttributeID = 1
INNER JOIN attributelink a2 ON o.ObjectID = a2.ObjectID AND a2.AttributeID = 2
GROUP BY o.ObjectID
Your test data doesn't show a whole lot about whether it works or not, but FWIW, here's the sqlfiddle.
Another way to do it is to use COUNT DISTINCT with HAVING and GROUP BY (sqlfiddle):
SELECT o.ObjectID
FROM objects o
INNER JOIN attributelink a ON o.ObjectID = a.ObjectID
WHERE a.AttributeID IN (1,2) --here you filter the rows on the attributes to test
GROUP BY o.ObjectID
HAVING COUNT(DISTINCT(a.AttributeID)) = 2 --# of attributes, means "having ALL"

query is not working correctly

I have these tables:
single_user_has_university_has_course
single_user_users_id_user university_has_course_university_id_university university_has_course_course_id_course first_year_school last_year_school grade
1 1 1 2000 2001 15
And
university_has_course
university_id_university course_id_course
1 1 <<< I want to select this
15 1
1 3
15 3
The problem is that the query below return all rows where course_id_course = 1 but ignore theuniversity_id_university = 1`. Then, will return two rows.
query:
SELECT A.first_year_school, A.last_year_school, A.grade, U.university, C.course, T.type_course
FROM single_user_has_university_has_course A
INNER JOIN university_has_course Q
ON A.university_has_course_course_id_course = Q.course_id_course
INNER JOIN university U
ON Q.university_id_university = U.id_university
INNER JOIN course C
ON Q.course_id_course = C.id_course
INNER JOIN type_course T
ON C.type_course_id_type_course = T.id_type_course
AND A.single_user_users_id_user = ?
You are joining the last table to the previous joins on 2 constraints C.type_course_id_type_course = T.id_type_course AND A.single_user_users_id_user = ?. I think you want to write it as a WHERE clause. So replace your last line with:
WHERE A.single_user_users_id_user = ?
But you're not also trying to filter university_id_university = 1, so you may want to add it to the WHERE clause as well:
WHERE A.single_user_users_id_user = ? AND Q.university_id_university = ?