I am trying to get all id under a typeid using id i want to use just one query to do this it can be done with an inner query like this.
SELECT id FROM users where typeId = (SELECT typeId FROM users where id = 5)
This will give me what i want but i have heard that inner query is slow so i would like to do this using join query.
users
id typeid name
1 2 a
2 2 b
3 1 c
4 3 d
5 3 e
That will be:
SELECT
a.id
FROM
users AS a
LEFT JOIN users AS b
ON a.typeId=b.typeId
WHERE
b.id = 5
-check this fiddle.
What you want I don't know with this query:
SELECT id FROM users where typeId = (SELECT typeId FROM users where id = 5)
You can simply write this query as:
SELECT t1.id
FROM users AS t1
LEFT JOIN users AS t2
ON t1.typeId = t2.typeId
WHERE t2.id=5
BUT I exclaimed that when you know id then why are you using the query to fetch the same id again. I think you are lacking some information.
Related
before posting this, I searched for an answer, but couldn't find the exact solution to my issue.
First of all, I am only assuming that I need INNER JOIN to solve it. FULL OUTER JOIN might also do the trick.
I have 3 different tables in one database.
TABLE 1
userId
roleID
1
1
2
2
TABLE 2
userId
userName
1
A
2
B
TABLE 3
roleId
roleName
1
X
2
Y
My goal is to write a query, which matches the userName from Table 2 with the roleName of Table 3 in the output. It should look like this:
Output
UserName
RoleName
A
X
B
Y
I can't figure it out. Any help would be appreciated.
Thank you very much!
EDIT:
so far I am trying to modify the following query to do the job:
SELECT * FROM
table1.roleId
INNER JOIN
table2.roleId
ON table1.roleId = table3.roleId
INNER JOIN
table2.userId
ON table2.userId = table3.roleId
Just join all tables together with following conditions:
Table 2 userId = Table 1 userId
Table 3 roleId = Table 1 roleId
Then select only those values which are relevant to you.
I think this should do the job:
select t2.userName, t3.roleName
from table2 t2
join table1 t1 on t2.userId = t1.userId
join table3 t3 on t3.roleId = t1.roleId;
The query above uses INNER JOINs to get the result.
If there are userIds without a roleId in Table 1, FULL OUTER JOIN would also return userNames with empty roleNames if such entries are present.
I wasn't sure how to word the title, but here is what I am trying to do. I have a table where the id can have multiple entries
id | number
___________
1 | 90
1 | 88
2 | 88
3 | 88
I want a query that will return all ids that don't contain the number 90, so only 2 and 3 in this example. I have tried the below, but it still returns the id of 1 since it also has a number of 88.
SELECT DISTINCT id FROM table WHERE number NOT IN (90)
One way of getting the result is by using NOT EXISTS. Basically what it does it it gets all ID which has 90 in the inner query and the NOT EXISTS only shows all ID not in the inner query.
SELECT A.*
FROM TableName a
WHERE NOT EXISTS (SELECT NULL
FROM TableName B
WHERE a.ID = b.ID
AND b.number = 90)
Here's a Demo.
An alternative is by using LEFT JOIN which yields the same result as above.
SELECT a.*
FROM TableName a
LEFT JOIN TableName b
ON a.ID = b.ID
AND b.number = 90
WHERE b.id IS NULL
Here's a Demo.
You can use subquery:
SELECT id
FROM table
WHERE id NOT IN (SELECT id FROM table WHERE number = 90)
You can use aggregation as illustrated below for better performance:
SELECT ID
FROM YourTable
GROUP BY ID
HAVING NOT INSTR(GROUP_CONCAT(`number`),'90');
Demo on SQL Fiddle.
here is some sample data:
ID Item
1 A
1 A
1 B
2 A
2 A
3 A
3 A
3 A
Question: Im trying to write code so that the only records that are selected are those of customer with ID 1 (ie a customer that has both product A and B). So results should look like this:
1 A
1 A
1 B
I've tried a lot of different things, but I am stuck. I tried self-join, but it doesnt produce what I want:
SELECT a.id, a.item
FROM table1 a Join table1 b on a.id=b.id
WHERE upper(a.item) = 'A'
AND upper(b.item) = 'B';
This will give me the right customer (ie customer 1) but it doesnt pull all 3 records. It just gives 1 row.
the closest similar question is
enter link description here
since you want to see which users match a certain condition and the fetch everything about those users - you need a nested query:
SELECT id,item FROM table1 WHERE id IN(
SELECT a.id
FROM table1 a Join table1 b on a.id=b.id
WHERE upper(a.item) = 'A'
AND upper(b.item) = 'B'
)
I took your working query, and used it in a WHERE clause for a more generic query - should do the trick for you
You could use your query as subselect to get the pid and then output all the pids rows. Like this:
SELECT id, item
FROM table1
WHERE id IN (SELECT a.id
FROM table1 a
JOIN table1 b
ON a.id=b.id
WHERE UPPER(a.item) = 'A'
AND UPPER(b.item) = 'B')
is it this what you are looking for? or may be I didn't understand your question.
SELECT a.pid, a.mname
FROM meds a
WHERE a.pid = 1
lets say I have 2 tables:
Table 1: (customers)
------------------------------------------
id | name | etc... | etc..
Table 2: (blockList)
------------------------
id
I want to know if each customer exists or not in blockList table as I'm looping thru customers table (in a single query, as a seperate field)
like this: SELECT * FROM customers, blockList ORDER BY id DESC
You need to use join, example :
SELECT c.*, b.id AS id_blocklist
FROM customers AS c
LEFT JOIN blocklist AS b ON b.id = c.id
ORDER BY c.id DESC
If you want only records in blocklist, use INNER JOIN
you must specify tables connection
SELECT * FROM customers as c, blockList as b WHERE c.id = b.id ORDER BY id DESC
I am trying to write a query that returns a users profile information, along with a count of occurrences of the user's ID in 2 columns from another table. An example below:
TableA
userID userName
1 UserA
2 UserB
TableB
LinkID leadID followID
1 1 2
2 1 3
3 2 1
Querying against UserID 1 I would expect to retrieve UserA, 2 (occurences of 1 in leadID), and 1 (occurences of 1 in followID). Any help with this is much appreciated, and thanks in advance.
You don't actually need to join for this - you can instead make three separate selects.
SELECT
(
SELECT userName
FROM TableA
WHERE userID = 1
) AS userName,
(
SELECT COUNT(*)
FROM TableB
WHERE leadID = 1
) AS count_leadID,
(
SELECT COUNT(*)
FROM TableB
WHERE followID = 1
) AS count_followID
Result:
userName count_leadID count_followID
UserA 2 1
SELECT a.userName,
SUM(IF(b.leadId = a.userId, 1, 0) as Leads,
SUM(IF(b.followId = a.userId, 1, 0) as Follows
FROM TableA a
TableB b
GROUP BY a.userName
SELECT a.userName,
b1.count(*) as leads,
b2.count(*) as follows
FROM TableA a
INNER JOIN TableB b1 on a.userID = b1.leadID
INNER JOIN TableB b2 on a.userID = b2.followID
GROUP BY a.userName
Depending on how mySQL optimizes and if you have an index on leadID and followID then this could speed up the query, especially if you're going to query just a few users rather than them all at the same time.