I got the following tables in my mysql database: http://i.gyazo.com/8da933221a20ecb0e8f4a8073a2a5b41.png
table: ACCOUNTS
ID NAMN
-- -----
1 Name1
2 Name2
3 Name3
4 Name4
table: SUBBED
SUBSCRIBER RECEIVER
---------- --------
1 4
1 3
I would like to select ID, NAME from ACCOUNTS where account X (lets say the one with ID 1) isn't subscribed to.
If you look at the image, you can see that the account with NAME = Name1 is subscribed to Name3 and Name4. In this case, the query should return the second row from ACCOUNTS where ID = 2.
Does someone know what the query might look like?
You probably just want to left join the tables together, and then find the ones that don't match. Something like this:
select a1.*
from accounts a1
left join subbed s1
on a1.id = s1.receiver
and s1.subscriber = 1
where s1.subscriber is null and a1.id <> 1;
demo here
You can try something like this.
SELECT ID, NAME FROM ACCOUNTS WHERE ID NOT IN (SELECT RECEIVER FROM SUBBED)
Hope it helps.
Select ID, NAME
From ACCOUNTS
Where ID Not In (Select RECEIVER
From SUBBED
Where SUBSCRIBER = (Your X))
Related
I have used a dynamic form builder plugin in WordPress. When I input some data from this form data inserted in the table. The table looks like below.
id entry_id field_id slug value
1 1 fld_7213434 name abc
2 1 fld_5474822 father def
3 1 fld_4459896 gender Female
4 1 fld_6364274 village_name ijk
20 2 fld_7213434 name J Jahan
21 2 fld_5474822 father Md Ali
22 2 fld_4459896 gender Female
23 2 fld_6364274 village_name ijk
I need to show this table's data like below
S.N. name father gender village_name
1 abc def Female ijk
2 J Jahan Md Ali Female Adabor
How can I do that?
One idea is to join the table to itself:
SELECT
t1.entry_id `S.N.`,
t1.value name,
t2. value father,
t3.value gender,
t4.value village_name
FROM mytable t1
LEFT JOIN mytable t2 ON t2.entry_id = t1.entry_id AND t2.field_id = 'fld_5474822'
LEFT JOIN mytable t3 ON t3.entry_id = t1.entry_id AND t3.field_id = 'fld_4459896'
LEFT JOIN mytable t4 ON t4.entry_id = t1.entry_id AND t4.field_id = 'fld_6364274'
WHERE
t1.field_id = 'fld_7213434'
Of course, instead of mytable you have to use your correct table name that you have not mentioned in your question.
A join is the Cartesion product (Wikipedia) of all involved tables. After filtering the resulting set of rows for those with the correct combinations (in this case, entry_id and field_id must be filtered), you get exactly what you want.
For performance reasons, you might want to have an index on the columns entry_id and field_id, but you'll find out.
I want to show a output table that counts all the users found in a table.
Basically I want the output to look like:
+-----------+-----------+
| user1 | user2 |
+-----------+-----------+
| 5 | 2 |
+-----------+-----------+
I'm just using a dummy table to test this. My query looks like this:
(
select
name as user1
from
users
where
name = 'root'
) UNION (
select
name as user2
from
users
where
name = 'not_root'
)
Which only outputs something like this:
+-----------+
| user1 |
+-----------+
| 5 |
| 2 |
+-----------+
EDITED
The main idea of the approach it's treat a table as two different virtual tables in subquery. We can make nested select statement e.g. (select count(*) as c from users where name = 'root') u1 MySql treats it as particular table named u1 with one row and one column named c.
select u1.c as root_cnt, u2.c as not_root_cnt
from (select count(*) as c from users where name = 'root') u1,
(select count(*) as c from users where name = 'not_root') u2
or
Moreover if you have select statement that returns only one row you can put nested selects directly in fields list
select (select count(*) as c from users where name = 'root') as root_cnt, (select count(*) as c from users where name = 'not_root') as not_root_cnt
Definite disadvantage of such approach it's extra subqueries. Method based on using case when construction free from such disadvantage.
Try this
SELECT
SUM(CASE WHEN Name = 'root' THEN 1 ELSE 0 END) user1,
SUM(CASE WHEN Name = 'not_root' THEN 1 ELSE 0 END) user2
FROM Users
You can use a case statement inside of count to get the counts in separate columns
select
count(case when name = 'root' then 1 end) user1,
count(case when name = 'not_root' then 1 end) user2
from users
where name in ('root','not_root')
It seems your query is wrong..
union won't combine the results of two queries in the way you have described above.
union would combine the result of two or more select statements but wouldn't "join" it.
You might want to use joins for this cause. still you wouldn't be able to put 5|2 in same row as it basically suggests --> fetch user 1 and user 2 values for one particular type
i think group by is a much better approach:
select user, count(user) from users group by user
I'm stuck for hours on an issue that might be pretty simple to solve but I'm just so lost...
I got 3 tables :
user
id name
----------
1 jack
2 john
...
car
id name
----------
1 ford
2 fiat
3 alfa
4 lada
...
user_car
id_user id_car
-----------------
1 2
1 4
2 1
2 2
2 3
For example, i want to get all users with cars which have id 1 AND 2 in the user_car table so I should get the id_user 2 only and I can't find the proper way to do it.
try this untested query:
select * from user join user_car car1 on id =car1.user_id
join user_car car2 on id =car2.user_id where car1.id_car=1 and car2.id_car=2
I would use UNION for this matter:
SELECT id as id_user from user where id in(1, 2)
UNION
SELECT id as id_car from car where id in (1, 2)
You can use COUNT to do this:-
SELECT user.name
FROM user
INNER JOIN user_car ON user.id = user_car.id_user
INNER JOIN car ON user_car.id_car = car.id
WHERE car.id IN (1,2)
GROUP BY user.name
HAVING COUNT(DISTINCT car.id) = 2
Note that this could be simplified to remove the need for the car table when you are just using the id of the car.
May be you want this
SELECT *
FROM USER
WHERE id IN
(SELECT id_user
FROM user_car
WHERE id_car=1 OR id_car=2);
I got 3 tables in my MYSQL bases and I have to compare how many time there are each user_ID in each of the 2 first table (table 1 and table 2)
here is my table 1:
user_ID
A
B
A
D
...
here is my table 2 :
user_ID
A
C
A
...
here is my table 3 (with link between user_ID and nickname) :
user_ID // nickname
A // Bob
B // Joe
C // Tom
...
I would like to get a result like this:
Nickname // count occurrences from Table 1 // count occurrences from table 2
Bob // 1 // 2
Joe // 4 // 0
Tom // 0 // 2
I did not succeed for instant to count separately from each table, I got a global result for each nickname :(
Could you help me to find the right MYSQL request ?
- ...
This type of query is a little tricky, because some names may not be in the first table and others may not be in the second. To really solve this type of problem, you need to pre-aggregate the results for each query. To get all the names, you need a left outer join:
select t3.name, coalesce(cnt1, 0) as cnt1, coalesce(cnt2, 0) as cnt2
from table3 t3 left outer join
(select name, count(*) as cnt1
from table1
group by name
) t1n
on t3.name = t1n.name left outer join
(select name, count(*) as cnt2
from table2
group by name
) t2n
on t3.name = t1n.name;
I know it's ambiguous subject, because if i know what exactly the name, google will give me the answer. And, sorry for the english.
Okay i have 3 tables looks like below
job job_hide user
--- -------- ----
id job_id id
name user_id name
let say we have 3 job with 1, 2, 3 as id and 2 user test1, test2 as id and 1 record for job_hide with 2 as job_id and test2 as user_id.
my question is, how i can use left join in case user test2 logged and i want to select all job except job with id 2 because job with id 2 has been added to job_hide table.
Thanks in advance.
You can simply use an is null condition for that purpose:
select job.* from job left join job_hide
on (job.id = job_hide.job_id and job_hide.user_id = 'test2')
where job_hide.job_id is null
SELECT id, name
FROM job
WHERE id NOT IN
( SELECT job_id
FROM job_hide AS jh
WHERE jh.job_id = job.id
AND jh.user_id = 2 --- 2 is the #id_of_the_logged_user
)