Moodle cohorts - SQLQuery to show all global groups in which a user is inserted - moodle

for a special Moodle-Workflow i need an SQL Query that tells me in which global groups (cohorts) a user with ID 100 is assigned or inserted. Who can help? Thanks a lot everyone for help.

Assuming your database prefix is default mdl_ :
SELECT c.* FROM mdl_cohort_members cm LEFT JOIN mdl_cohorts c ON (c.id = cm.cohortid) WHERE cm.userid = 100

Related

Postgres - How to join on two columns?

I have a table containing account information:
And another table, containing transactions information:
I'd like to retrieve both the titles from transactions.from_acc_id and transactions.to_acc_id
So far, I'm only able to retrieve either one or the other with the following JOIN:
SELECT transactions.transaction_type,
transactions.from_acc_id,
transactions.to_acc_id,
transactions.amount,
account.title AS "ACCOUNT DESTINATION"
FROM transactions
JOIN account
ON transactions.to_acc_id = account.acc_id
This gives me the title of transactions.to_acc_id.
How can I add another field containing the title of transactions.from_acc_id with the same SELECT statement, please ?
Thanks
EDIT: I'd like to keep all field from the Select statement, adding the title of transactions.from_acc_id where is pertinent
You join your account table in twice and give each instance its own alias. Furthermore, to ensure that every record from transactions table shows up and only those records from your accounts table (both source and destination), use a LEFT OUTER JOIN instead of your implicit INNER JOIN that you are currently using.
SELECT transactions.transaction_type,
transactions.from_acc_id,
transactions.to_acc_id,
transactions.amount,
dest.title AS "ACCOUNT DESTINATION",
src.title AS "ACCOUNT SOURCE"
FROM transactions
LEFT OUTER JOIN account as dest
ON transactions.to_acc_id = dext.acc_id
LEFT OUTER JOIN account as src
ON transactions.from_acc_id = src.acc_id
For more information on types of joins that are available in most databases, check out W3Schools SQL Joins page
Simply join the table twice. Use table aliases to tell the source account from the target account.
SELECT
t.transaction_type,
t.from_acc_id,
t.to_acc_id,
t.amount,
from_acc.title AS from_account,
to_acc.title AS to_account
FROM transactions t
LEFT JOIN account from_acc ON from_acc.acc_id = t.from_acc_id
LEFT JOIN account to_acc ON to_acc.acc_id = t.to_acc_id

Need help for Filtering the data in sql

i have a table that contain s.no Id and Amount and accCode.
s.no-------------id--------------Amount--------accCode
1----------------2---------------20-------------2.1
2----------------1---------------30-------------2.1
3--------------- 5---------------20-------------3.1
4----------------1---------------30-------------2.1
5----------------3---------------40-------------3.1
6----------------2---------------20-------------2.1
i need all the record that have a common Amount and accCode and id. In this case i need to show the data of S.NO 2 and 4, and also 1 and 6 as they have similar value. If Possible it would be better the similar data come orderly. Is this one possible through Sql? Please give some hints i am stuck with this one.thaks in advance.
One solution could be, assuming that your table is named "test"
select t1.*, t2.[s.no] as MatchSNo from test t1, test t2
where t1.id = t2.id and t1.amount = t2.amount
and t1.acccode = t2.acccode and t1.[s.no] <> t2.[s.no]
order by t1.id, t1.Amount, t1.accCode, [s.no]

a dual variable not in statement?

I have the need to look at two tables that share two variables and get a list of the data from one table that does not have matching data in the other table. Example:
Table A
xName
Date
Place
xAmount
Table B
yName
Date
Place
yAmount
I need to be able to write a query that will check Table A and find entries that have no corresponding entry in Table B. If it was a one variable issue I could use not in statement but I can't think of a way to do that with two variables. A left join also does not appear like you could do it. Since looking at it by a specific date or place name would not work since we are talking about thousands of dates and hundreds of place names.
Thanks in advance to anyone who can help out.
SELECT TableA.Date,
TableA.Place,
TableA.xName,
TableA.xAmount,
TableB.yName,
TableB.yAmount
FROM TableA
LEFT OUTER JOIN TableB
ON TableA.Date = TableB.Date
AND TableA.Place = TableB.Place
WHERE TableB.yName IS NULL
OR TableB.yAmount IS NULL
SELECT * FROM A WHERE NOT EXISTS
(SELECT 1 FROM B
WHERE A.xName = B.yName AND A.Date = B.Date AND A.Place = B.Place AND A.xAmount = B.yAmount)
in ORACLE:
select xName , xAmount from tableA
MINUS
select yName , yAmount from tableB

mysql subquery in single table

I have 2 columns in an "entries" table: a non-unique id, and a date stamp (YYYY-MM-DD). I want to select "all of the entry id's that were inserted today that have never been entered before."
I've been trying to use a subquery, but I don't think I'm using it right since they're both performed on the same table. Could someone help me out with the proper select statement? I can provide more details if need be.
Disclaimer: I don't have access to a mysql database right now, but this should help:
select
e.id
from
entries e
where
e.date = curdate() and
e.id not in
(select id from entries e2 where e2.date < e.date)

T-SQL - How to write query to get records that match ALL records in a many to many join

(I don't think I have titled this question correctly - but I don't know how to describe it)
Here is what I am trying to do:
Let's say I have a Person table that has a PersonID field. And let's say that a Person can belong to many Groups. So there is a Group table with a GroupID field and a GroupMembership table that is a many-to-many join between the two tables and the GroupMembership table has a PersonID field and a GroupID field. So far, it is a simple many to many join.
Given a list of GroupIDs I would like to be able to write a query that returns all of the people that are in ALL of those groups (not any one of those groups). And the query should be able to handle any number of GroupIDs. I would like to avoid dynamic SQL.
Is there some simple way of doing this that I am missing?
Thanks,
Corey
select person_id, count(*) from groupmembership
where group_id in ([your list of group ids])
group by person_id
having count(*) = [size of your list of group ids]
Edited: thank you dotjoe!
Basically you are looking for Persons for whom there is no group he is not a member of, so
select *
from Person p
where not exists (
select 1
from Group g
where not exists (
select 1
from GroupMembership gm
where gm.PersonID = p.ID
and gm.GroupID = g.ID
)
)
You're basically not going to avoid "dynamic" SQL in the sense of dynamically generating the query at query time. There's no way to hand a list around in SQL (well, there is, table variables, but getting them into the system from C# is either impossible (2005 & below) or else annoying (2008)).
One way that you could do it with multiple queries is to insert your list into a work table (probably a process-keyed table) and join against that table. The only other option would be to use a dynamic query such as the ones specified by Jonathan and hongliang.