Inner join of same table with conditions - mysql

So my problem is as follow, I have a table in MySQL with a UserId column and an ObjectId column (its a many to many relationship), and what I would like is to have is a query that gives me the list of objects that user X and Y share. Not sure how to make the joins to make this happen.

Use query something like below using self join
Select columns from table t1 join table t2 on t1.objectid=t2.objectid where t1.userid=X and t2.userid=Y

Related

MySQL Join through an intermediary table

I am building a query and I need to select from the log table multiple columns, my issue is that i'm trying to find a way to select a column that has an FK in a table that has an FK to another table.
I have:
log.number_id,
numbers.number_id
numbers.country_id,
countries.country_id
Query is almost done, my only issue is that I need to show countries.country_id through an intermediary table FK numbers.country_id, I believe this is an INNER JOIN yet I have no idea how to create the concatenation, I searched google for this, yet I couldn't find something like a general formula of how to execute such an intermediary join.
I'm guessing you're looking for something like this.
Basically joining the table with both id's to the other tables on the common id.
SELECT log.*, ctry.*
FROM numbers AS ctrylog
JOIN log
ON log.number_id = ctrylog.number_id
JOIN countries AS ctry
ON ctry.country_id = ctrylog.country_id

Select from a table the ones that don't have a relationship with another table

The specific problem is listing the names of the teachers that never graded.
I have 'teachers' table with the columns 'Name' and 'ID'.
And 'grades' table with the column 'IDTeacher' and 'Grade'.
Don't get why this doesn't work:
Select Name from teachers where not exists(Select * from grades, teachers)
You can just join it with the grades table and use the ones where the join returns "null" for the right side:
SELECT
name
from
teachers t
LEFT JOIN
grades g
on
t.teacher = g.teacher
WHERE
ISNULL(g.teacher)
edit: Thought about a right join instead, but no, the right join might not work, if the teacher has no entry in the grades table. (Then you would miss him completely, even if he is in the teacher table)
You could also use WHERE IN for this:
SELECT
name
FROM
teachers
WHERE
name
NOT IN (SELECT name from grades)
BUT the MySQL Optimizer will rewrite this to exactly the correlated subquery #Gordon Linoff has written. Using WHERE NOT IN is just easier to read imho.
Your query does work, it just doesn't do what you think it should. The subquery creates a cartesian product between the two tables. If both tables have rows, then the cartesian product has rows and the where clause will always be true.
You can take this approach, but you need a correlated subquery:
Select Name
from teachers t
where not exists (Select 1 from grades g where g.idteacherid = t.id);
Note that this query only has one table in the subquery.
There are other ways to write this query, but this seems to be the approach you are heading in. And, not exists is a very reasonable approach.

select from database table rows that are not in another table

I have a table of 'entries' in a MYSQL database. I have another table that records activity on those entries, with the id of the entry as a foreign key. I want to select from my first table entries that do not appear in the second table.
How can I use SQL to make this happen? Do I have to iterate through both tables and compare every entry with every other entry? Is there an easier way to do this?
ex. I have a table with an entry data column and a user name column. I have another table with an entry id column and a user id column. I want to select from my first table all of the entries which do not appear in the second table with a given user id.
Thanks ahead of time. I have been struggling with this experiment for a while. I imagine I have to join the two tables somehow?
Several ways to achieve this, NOT IN, NOT EXISTS, LEFT JOIN / NULL check. Here's one with NOT EXISTS:
SELECT *
FROM FirstTable T
WHERE NOT EXISTS (
SELECT *
FROM SecondTable T2
WHERE T.Id = T2.Id
)
From what I understand, you want to select all rows where the foreign key doesn't match anything in the other table. This should do the trick:
SELECT *
FROM Data A
RIGHT JOIN Entry B
ON A.ID = B.ID
WHERE A.ID IS NULL
Here's a handy chart that illustrates how to use joins for stuff like this.
You can also use NOT IN, and the mechanics for this one are actually a bit easier to understand.
SELECT *
FROM Data A
WHERE A.ID NOT IN (SELECT ID FROM Entry)

Getting a string from a referenced table

I am relativly new to the SQL language. I can do a basic select, but for performance increase, I'd love to know if it is possible to merge the two queries I am doing at the moment into one.
Scenario: There are two tables. Table one has a few columns, one of them is a VARCHAR(45) named 'user', and another one is a INT which is called 'gid'. In the second table, there is a primary key column called 'gid' (INT) and a column called 'permissions' which is a TEXT column and it contains values seperated by ';'.
I have a user name, and want the text in the permissions column. The current way I do it is by fetching the gid of the first table, then doing a second query with the gid to get the permissions.
I've heard there are other ways to do this, and I have searched on Google, but I'm not sure what I should do.
EDIT:
Like this:
select t2.permissions
from table1 t1, table2 t2
where t1.user = '<SPECIFIED NAME>'
and t1.gid = t2.gid;
or you could use INNER JOIN syntax:
select t2.permissions
from table1 t1
inner join table2 t2 on t1.gid = t2.gid
where t1.user = '<SPECIFIED VALUE>'
To do this you use a JOIN. A join connects two tables in a select statement.
Like this
select *
from usertable u
join permissiontable p on u.gid = p.gid
This will give you all the columns from both tables with the id column joined. You can treat the joined table just like any table (eg select a sub-set of columns in the select list, add a where clause, etc).
You can read more about joins in any intro sql book or doing a google search.

create a new table from a full join

I want to create a new table that includes certain columns from two existing tables. Some columns are in both existing tables, but not all. The column "unique_number" is in both tables. What I want is to check if the same unique_number exists in both tables, and if so then make a single row with all of the columns (from both tables) that correspond to that unique_number. If the unique_number is not in both existing tables then I don't care about including that in my new table.
I can do this in SAS, but I am being forced to do this in SQL with little background knowledge of the program.
It sounds that you need an INNER JOIN instead of a FULL JOIN. This is a way of doing what you want:
SELECT A.unique_number,
A.col1FromA, -- list the other columns from TableA here
B.col1FromB -- list the other columns from TableB here
INTO TableC
FROM TableA A
INNER JOIN TableB B
ON A.unique_number = B.unique_number
An inner join on the "unique_number" column should be all you need, if your description is accurate.
select t1.unique_number, t1.other_column, t2.something_else
into new_table_name
from one_table t1
inner join other_table t2
on t1.unique_number = t2.unique_number;
This glosses over the complication that "some columns are in both tables", and that those columns that are in both tables might have different values. Give some thought to that.
Select...into syntax