Find all user in table A that are also in Table B - sql

I have table A with fields user1 and user2 and table B with user3 and user4 pairs.
I want to be able to find any rows from table A where the user1/user2 combination is also in table B.
The order doesn't matter. For example table A with user1=Mike, user2=Joe would match table B with user3=Joe and user4=Mike.

I'd probably just use an explicit or in a join:
select user1, user2
from tableA join tableB on
(user1=user3 and user2=user4) or
(user1=user4 and user2=user3)
...but take it with a grain of salt. I've been accused of over-using joins, probably with at least a little reason.

select a.user1, a.user2 from a, b
where (a.user1 == b.user3 and a.user2 == b.user4)
or (a.user1 = b.user4 and a.user2 = b.user3);

Related

Select rows from table where a certain value in a joined table does not exist

I have two tables, playgrounds and maintenance, which are linked with a foreign key. Whenever there is a maintenance on a playground, it will be saved in the table and connected to the respective playground.
Table A (playgrounds):
playground_number
Table B (maintenance):
playground_number (foreign key),
maintenance_type (3 different types),
date
What I now want is to retrieve all the playgrounds on which a certain type of maintenance has NOT been performed yet IN a certain year. For instance all playgrounds that do not have a maintenance_type = 1 in the year 2022 connected yet, although there could be multiple other maintenance_types because they are more frequent.
This is what I have tried (pseudo):
SELECT DISTINCT A.playground_number
FROM table A
JOIN table B ON A.playground_number = B.playground_number (FK)
WHERE NOT EXISTS (SELECT B.maintenance_type FROM table B
WHERE B.maintenance_type = 1 AND year(B.date) = 2022
However this will return nothing as soon as there is only one entry with maintenance_type 1 within the table.
I am struggling with this query for a while, so would appreciate some thoughts :) Many thanks.
You need to correlate the exists subquery to the outer B table. Also, you don't even need the join.
SELECT DISTINCT a.playground_number
FROM table_a a
WHERE NOT EXISTS (
SELECT 1
FROM table_b b
WHERE b.playground_number = a.playground_number AND
b.maintenance_type = 1 AND
YEAR(b.date) = 2022
);
Please consider this. I don't think you need JOIN.
SELECT DISTINCT A.playground_number
FROM table A
WHERE A.playground_number NOT IN (SELECT B.playground_number FROM table B
WHERE B.maintenance_type = 1 AND year(B.date) = 2022)
Please let me know if I understand it incorrectly.

Merge/Update records from the same table

I have two recordsets from a single table:
SELECT * FROM userconfig WHERE userid = 'user1'
AND
SELECT * FROM userconfig WHERE userid = 'user2'
I would like to update or merge (whatever is easier) one column from a specific user's recordset into the recordset of the other user. IE: user1 has a column configvalue whose values I want to insert into user2's configvalue column. I need both columns to have the same value. What is the simplest way to achieve this?
Update
user2.userkey = user1.userkey,
user2.uservalue = user1.uservalue
FROM
MyTable user2 INNER JOIN MyTable user1 ON user1.YourKey = user2.YourKey
The same table, MyTable (replace with your table name). Join this table back to itself based on the YourKey column (replace YourKey with the column where the join matches).
Then simply assign the alias values of user1 into user2. Aliases and joining back to the same table is the key.

How can I merge 2 access tables, keeping all data from table a and updated data from table b

Table A has a population of names and unique ids. Table B has the same unique ids and names. The majority of the names in table B are null, but some have an updated name. I want to merge the two tables so I get the old names from table A and new names from table B if they exist. Basically layer table B on top of table A to capture changes to the names.
I've done something like this in sas, but am having a problem in Access. merging via sas is no longer an option. can this be done in access?
You can do this in SQL using theIIFandISNULLfunctions to select the name from the correct table (from TableA if TableB is null, otherwise from TableB). If your tables has two fields:(id,the_name)a query could look like this:
SELECT a.id, IIF(ISNULL(b.the_name), a.the_name, b.the_name) AS the_name
INTO TableC
FROM TableA a
INNER JOIN TableB b ON a.id = b.id

Simple SQL Select from 2 Tables (What is a Join?)

I'm new to SQL. I have a simple problem with getting the results from two different tables.
I have two tables in a database. The first table has a column with an id reference, which corresponds to rows in the second table. What SELECT do I need to perform to get a result such that the ids are repalced by all of the values in the second table. To visualize the tables I am discussing:
TABLE_USERS
===========
id username group
-- -------- -----
1 jim A
2 alice A
3 brandon B
TABLE_GROUPS
============
id groupname members
-- --------- -------
A designer 134
B photographer 39
DESIRED_SELECTION
=================
id username group
-- -------- -----
1 jim designer
2 alice designer
3 brandon photographer
Thanks!
You do, in fact, want to JOIN the two tables:
SELECT * FROM
TABLE_USERS LEFT JOIN TABLE_GROUPS
ON TABLE_USERS.group = TABLE_GROUPS.id
The trick of joining tables is to find the values that must match in the two tables, and use the on to tell SQL to match them. This table has a ID column to let you do that = you will join the table, ON, and then list the values that need to be equal.
If you do not want all of the columns in both tables, you can simply list only the columns you need in your final query. This means that instead of Select *, you list the columns you want. As shown below, if a column appears with the same name in both tables, you need to prepend the table name, so that SQL know which value you want.
SELECT TABLE_USERS.ID, Username, Groupname
FROM TABLE_USERS
LEFT JOIN TABLE_GROUPS
ON TABLE_USERS.group = TABLE_GROUPS.id
You want a JOIN:
SELECT
u.id,
username,
groupname
FROM
TABLE_USERS AS u
LEFT JOIN TABLE_GROUPS AS g
ON u.group = g.id

How can I remove duplicate relationships in a MySQL table data?

I have a table "friends" with "user1" and "user2" columns.
How can I remove duplicate relationships?
For example, if I already have
user1=1 and user2=3,
how can I remove user1=3 and user2=1 values?
Could I enforce that in MySQL with a unique index?
With this query you can find duplicate relationships
SELECT *
FROM friends d
WHERE EXISTS (
SELECT 1
FROM friends f
WHERE f.user1 = d.user2
AND f.user2 = d.user1
AND f.user2 < f.user1
)
I recommend put the lower userId for user1 and create a unique index
For example, don't allow rows, where user1 > user2.
Depending on the other columns in your table you could create a composite primary key over the 2 columns to enforce the uniqueness.
To find the duplicates, just join the table to itself. It is not difficult.