Multiple tables with JOINS - mysql

I have 2 tables, in table1 I have columns user_d and user_i, the next code, return only name for user_d.
SELECT table1.*, table2.name as nameuser FROM table1 INNER JOIN
table2 ON table1.user_d=table2.id where table1.id=1
How to return username for table1 user_i column too?

Try this
select distinct(tickets.id),(select name from users where user_d=id) as user_d,
(select name from users where user_i=id) as user_i from tickets INNER JOIN
users ON tickets.user_d=users.id order by tickets.id asc

Related

JOIN Only if One Matching Record in both tables

Essentially I need to only LEFT JOIN on the 2 tables if there is one customerid matching in table2. If there is more than 1 record matching table2 it should not count as a match.
Currently I am doing the following:
SELECT DISTINCT
table1.customerid,
table1.name AS customer,
table2.locationid,
table2.locationname
FROM
table1
LEFT JOIN table2 ON table1.customerid = table2.customerid
WHERE
ORDER BY
name ASC
The issue is it matches on all records.
To clarify -- if customerid is in table2 more than once, it should not join on the match, only if customerid is listed once for a record in table2.
How can this be done?
Join with a subquery that only returns customer IDs that have count = 1.
SELECT DISTINCT
table1.customerid,
table1.name AS customer,
table2.locationid,
table2.locationname
FROM table1
LEFT JOIN (
SELECT customerid, MAX(locationid) AS locationid, MAX(locationname) AS locationname
FROM table2
GROUP BY customerid
HAVING COUNT(*) = 1
) AS table2 ON table1.customerid = table2.customerid
ORDER BY name ASC
Note that using LEFT JOIN means that if locationid is not in table2 at all, the query will still return a row for that location, with NULL in the table2 fields. And with this change, it will also return those null rows when there's more than 1 matching row. If you want those rows omitted from the results entirely, use INNER JOIN rather than LEFT JOIN.

Mysql need to get one column two times from table1 and sort them by two different columns of table2

So here is the thing, I have two tables:
table1 has columns intUsersID, varUsersName
table2 has columns intCouriers, intResponsible
intCouriers (have some numbers of intUsersID that are Couriers), and intResponsible (have some numbers of intUsersID that are Responsible)
In my query I must see User Names of Couriers and of the Responsible persons
something like that:
SELECT
table1.varUsersName 'Couriers',
table1.varUsersName 'Responsible'
FROM
table1
LEFT JOIN
table2 ON table2.intCouriers = table1.intUsersID
And then I need some how to subquery or join this "table1.varUsersName 'Responsible'", to get also 'Reponsible' persons. Please help me.
Should be this
SELECT table1.varUsersName 'Couriers', table2.varUsersName 'Responsible'
FROM table1
INNER JOIN table3 on table1.intUsersID = table3.intCouriers
INNER JOIN table1 as Table2 on table2.intUsersID = table3. intResponsible
SELECT Couriers.varUsersName as "Couriers",
Responsible.varUsersName as "Responsible"
FROM `table2` t2
LEFT JOIN table1 Couriers on Couriers.intUsersID = t2.intCouriers
LEFT JOIN table1 Responsible on Responsible.intUsersID = t2.intResponsible

MySQL select count of another table's rows where the IDs match

I'm trying to select from one table with the count of another table where the id matches the original tableID sort of like:
select *, (count(0) from table2 where table2.table1ID = table1.table1ID) count
from table1
What's the mySQL syntax for this?
SELECT COUNT(b.*) FROM table1 as a
LEFT JOIN table2 as b ON a.tableID = b.tableID
select
table1.*,
if(table2.table1ID is null,0,count(*))
from
table1
left join table2 on table1.table1ID = table2.table1ID
group by
table1.table1ID;

How to combine 2 columns from 2 tables and subtract duplicates

I have 2 tables t1 and t2. Each have a customer ID column. What I am looking for is to join the 2 columns and SUBTRACT the duplicates.
My EG:
Table1 and Table2 with the IDs for each
I have tried a union query. The result I am left with is ID = 1,2,3,4,5,6,7,8,9,10. Where, what I'm after is subtracting 1-5 from Table2 and the result = 6,7,8,9,10.
I hope that makes sense and that someone is able to help. Sorry if this is a bit too simple compared to what you're all used to.
In SQL Server you can use the EXCEPT operator:
select ID
from Table2
except
select ID
from Table1
Mysql does not support it though. Using a an in clause or a left join would work in both servers:
--Using In clause
SELECT ID
FROM Table2
WHERE ID NOT IN
(
SELECT ID
FROM Table1
);
--Using join
SELECT Table2.ID
FROM Table2
left join Table1
on Table2.ID = Table1.ID
where Table1.ID is null
Use left outer join
select * from t1 left outer join t2 on t1.customerid = t2.customerid

UNION or JOIN for this query

I have a $member_id, there are 5 tables which this member details are stored in those tables in multiple rows.
For getting out this user data from those tables, I can use JOIN and UNION:
//Using JOIN:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id=table2.id LEFT JOIN table3 ON table2.id=table3.id AND table1.member_id = '$member_id'
//USING UNION
(SELECT a FROM t1 WHERE member_id = '$member_id')
UNION
(SELECT a FROM t2 WHERE member_id = '$member_id')
ORDER BY a LIMIT 10;
Which one is preffered? which one has the better performance? what's the true way while you want to get some information from many tables which are related with foreign keys?
If you need all columns in same row you should use join. For a row by column you should use union. This is the criteria. They are no secret.
Remember to use left joins if this column don't appear in all tables:
SELECT table0.id as a0, table1.id as a1, table2.id as a2, ...
FROM
(select '$member_id' as id from dual ) table0
LEFT outer join table1 ON table1.id=table0.id
Left outer JOIN table2 ON table1.id=table2.id
LEFT outer JOIN table3 ON table2.id=table3.id
Also, in union, you can tag each row:
//USING UNION
(SELECT 't1' as t, a FROM t1 WHERE member_id = '$member_id')
UNION ALL
(SELECT 't2' as t, a FROM t2 WHERE member_id = '$member_id')
ORDER BY a LIMIT 10;
If you have the same structure and want to remove duplicate rows you can use UNION otherwise it's better to use JOIN statement