I have the following relationship,
And wanted to know how I can get the team a game winner?
SELECT
m.id_match,
IF((id_home_team = ? AND score_home_team > score_away_team) OR
(id_away_team = ? AND score_home_team < score_away_team), 1, 0) AS won
FROM matches m
JOIN results r ON(r.id_match = m.id_match)
WHERE
id_home_team = ? OR id_away_team = ?
Replace the ? with your team's ID.
In SQL Fiddle.
Related
I am trying to figure out how to get all tasks in this case that two of the fields equal a certain value or they exist in the other table?
Here is the query:
SELECT TASKS.task_id, TASKS.task_title, TASKS.task_description, TASKS.task_assigned_name, TASKS.task_assigned_phone_number, TASKS.task_due_date_time, TASKS.task_category
FROM TASKS
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WHERE EXISTS (SELECT WATCHERS.task_id
FROM WATCHERS
WHERE WATCHERS.task_id = TASK.task_id AND
WATCHERS.watcher_user_id = ?
)
);
This is not returning anything even though I am expecting a result from my db.
You seem to have an error in your syntax. You have too many WHEREs:
SELECT t.task_id, t.task_title, t.task_description, t.task_assigned_name, t.task_assigned_phone_number, t.task_due_date_time, t.task_category
FROM TASKS t
WHERE t.task_complete = 1 AND
(t.task_creator_id = ? OR
t.task_assigned_user_id = ? OR
EXISTS (SELECT 1 -- the return value is immaterial
FROM WATCHERS w
WHERE w.task_id = t.task_id AND
w.watcher_user_id = ?
)
);
The WHERE before EXISTS is not appropriate.
Your query should be returning an error. Be sure to check for errors!
Have you tried using a join?
SELECT TASKS.task_id,
TASKS.task_title,
TASKS.task_description,
TASKS.task_assigned_name,
TASKS.task_assigned_phone_number,
TASKS.task_due_date_time,
TASKS.task_category
FROM TASKS
JOIN WATCHERS on WATCHERS.task_id = TASK.task_id
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WATCHERS.watcher_user_id = ?);
I'm not sure if that's the logic you are looking for.
besides the extra where in your query, looks like you may have an extra closed parenthesis.
This sql matches the result set you posted in your duplicate post that has sample data and result:
SELECT t.task_id, t.task_complete, t.task_creator_id, t.task_assigned_user_Id
FROM tasks t
WHERE t.task_complete = 1 AND
(
t.task_creator_id = 8
OR t.task_assigned_user_id = 8
OR EXISTS
(
SELECT w.task_id
FROM watchers w
WHERE w.task_id = t.task_id
AND w.watcher_user_id = 8
)
)
I am trying to figure out how to get all tasks in this case that two of the fields equal a certain value or they exist in the other table?
Here is the query:
SELECT TASKS.task_id, TASKS.task_title, TASKS.task_description, TASKS.task_assigned_name, TASKS.task_assigned_phone_number, TASKS.task_due_date_time, TASKS.task_category
FROM TASKS
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WHERE EXISTS (SELECT WATCHERS.task_id
FROM WATCHERS
WHERE WATCHERS.task_id = TASK.task_id AND
WATCHERS.watcher_user_id = ?
)
);
This is not returning anything even though I am expecting a result from my db.
You seem to have an error in your syntax. You have too many WHEREs:
SELECT t.task_id, t.task_title, t.task_description, t.task_assigned_name, t.task_assigned_phone_number, t.task_due_date_time, t.task_category
FROM TASKS t
WHERE t.task_complete = 1 AND
(t.task_creator_id = ? OR
t.task_assigned_user_id = ? OR
EXISTS (SELECT 1 -- the return value is immaterial
FROM WATCHERS w
WHERE w.task_id = t.task_id AND
w.watcher_user_id = ?
)
);
The WHERE before EXISTS is not appropriate.
Your query should be returning an error. Be sure to check for errors!
Have you tried using a join?
SELECT TASKS.task_id,
TASKS.task_title,
TASKS.task_description,
TASKS.task_assigned_name,
TASKS.task_assigned_phone_number,
TASKS.task_due_date_time,
TASKS.task_category
FROM TASKS
JOIN WATCHERS on WATCHERS.task_id = TASK.task_id
WHERE TASKS.task_complete = 1 AND
(TASKS.task_creator_id = ? OR
TASKS.task_assigned_user_id = ? OR
WATCHERS.watcher_user_id = ?);
I'm not sure if that's the logic you are looking for.
besides the extra where in your query, looks like you may have an extra closed parenthesis.
This sql matches the result set you posted in your duplicate post that has sample data and result:
SELECT t.task_id, t.task_complete, t.task_creator_id, t.task_assigned_user_Id
FROM tasks t
WHERE t.task_complete = 1 AND
(
t.task_creator_id = 8
OR t.task_assigned_user_id = 8
OR EXISTS
(
SELECT w.task_id
FROM watchers w
WHERE w.task_id = t.task_id
AND w.watcher_user_id = 8
)
)
i need to fetch the record with some condtions from two table
SELECT `tbui`.`user_id`, `tbui`.`first_name`,`tbui`.`last_name`,`tbui`.`education_level`,`tbui`.`sex`,`tbui`.`country_of_origin`,`tbui`.`city`,`tbui`.`state`,`tbui`.`country`,`tbui`.`occupation`,`tbui`.`about`,TIMESTAMPDIFF(YEAR,`tbui`.`age`,CURDATE()) as age
FROM `tb_preference_dropdown` as `tbpda`
RIGHT JOIN `tb_user_answers` as `tbua` ON `tbpda`.`question_id` = `tbua`.`question_id`
RIGHT JOIN `tb_preference_questions` as `tbpa` ON `tbpa`.`user_id` = `tbpda`.`user_id`
RIGHT JOIN `tb_user_info` as `tbui` ON `tbui`.`user_id` = `tbua`.`user_id`
WHERE `tbpda`.`user_id` = 113
AND TIMESTAMPDIFF(YEAR,`tbui`.`age`,CURDATE()) >=`tbpa`.`min_age_required` AND TIMESTAMPDIFF(YEAR,`tbui`.`age`,CURDATE()) <= `tbpa`.`max_age_required`
AND `tbui`.`country_of_origin` = `tbpa`.`country_of_origin`
AND `tbua`.`user_id` != 113
AND `tbui`.`sex` != 'Female'
HAVING SUM(IF(tbpda.question_id = 1,IF(tbpda.answer_id !=0,IF(tbua.question_id =tbpda.question_id AND `tbua`.`answer_id` = tbpda.answer_id,tbua.user_id,''),true),'')) > 0
all the things working perfectly except:
HAVING SUM(IF(tbpda.question_id = 1,IF(tbpda.answer_id !=0,IF(tbua.question_id =tbpda.question_id AND `tbua`.`answer_id` = tbpda.answer_id,tbua.user_id,''),true),'')) > 0
i am getting the records which even don't match with the above condition
You are missing GROUP BY clause, re-write the query
I have a rails join table between 2 models superhero and superpower. Now I have 3 different superpower id and I want all the superheroes which have all the selected superpowers
To do that I'm trying to do the following:
matches = Superhero.all
matches = matches.joins(:superpowers).where('superpowers.id = ?', 17).where('superpowers.id = ?', 12).where('superpowers.id = ?', 6)
But this gives me an empty object even though I have superheroes which have all the given superpowers in my join table
The query generated from the above is:
SELECT "superheroes".* FROM "superheroes" INNER JOIN "superheroes_superpowers" ON "superheroes_superpowers"."superhero_id" = "superheroes"."id" INNER JOIN "superpowers" ON "superpowers"."id" = "superheroes_superpowers"."superpower_id" WHERE (superpowers.id = 17) AND (superpowers.id = 17) AND (superpowers.id = 12) AND (superpowers.id = 6)
So weirdly it tries to check for the superpower with id 17 twice (but it shouldn't affect the result I think) and the rest of the query seems to be correct.
try using an in clause
superpowers_ids = [17,12,6]
matches = Superhero.all
matches = matches.joins(:superpowers).where('superpowers.id in (?)', superpowers_ids)
Superhero.joins(:superpowers).where(superpowers: { id: [17,12,6] } )
This gives the following SQL query (formatted for readibility):
SELECT "superheros".*
FROM "superheros"
INNER JOIN "superhero_superpowers" ON "superhero_superpowers"."superhero_id" = "superheros"."id"
INNER JOIN "superpowers" ON "superpowers"."id" = "superhero_superpowers"."superpower_id"
WHERE "superpowers"."id" IN (17, 12, 6)
I'm building a website for an school and i need a list of users that don't have 2 parents asigned. This would be the SQL query:
SQL
SELECT * FROM users
WHERE user_rol = 4
AND user_id IN
(SELECT parent_user_id FROM user_parents
GROUP BY parent_user_id
HAVING COUNT(parent_user_id) < 2);
I'm trying to use LINQ for the same query but I don't know how to use HAVING with LINQ. This has been my closer try for the moment.
SUB-QUERY FOR IN
List<long> usersWithTwoParentsIds = (from currentStudents in contexto.user_parents
select currentStudents.parent_user_id).ToList<long>();
--HELP! having count(currentStudents.parent_user_id) < 2
QUERY
List<vw_user> userList = (from currentStudents in contexto.vw_user
where !usersWithTwoParentsIds.Contains(currentStudents.user_id)
&& currentStudents.group_id == groupID select currentStudents).ToList<vw_user>()
Can anybody give a clue? Thanks :)
Something like this:
var usersWithTwoParentsIds = (
from userParent in contexto.user_parents
group userParent by userParent.parent_user_id into userParentGroups
where userParentGroups.Count() < 2
select userParentGroups.Key)
.ToList();
Assuming the key relations are proper in your data context:
var dat = Context.Users.Where( u => u.Parents.Count() < 2 );