handling null values on select query mysql - mysql

table stockholders
stockholder_id election_id user_id
1 1 1
2 1 2
3 2 3
table users
user_id user_type
1 1
2 1
3 1
4 1
5 1
select
*
from
tbl_users
left join tbl_stockholders
on tbl_stockholders.user_id = tbl_users.user_id
where
user_type=1
and stockholders.user_id is null
and election_id <> 1
i want to search election_id not equal to 1 and user type equal to 1
stockholder_id election_id user_id user_type
3 2 3 1
null null 4 1
null null 5 1
this is an update
sorry my problem should be excluding the tbl_stockholders from the tbl_users with the parameter election_id.. because a problem exist when i have a duplicate user_id
table stockholders
stockholder_id election_id user_id
1 1 1
2 1 2
3 2 3
4 1 3
in the previous answer this is the result when election_id<>2
stockholder_id election_id user_id user_type
3 1 3 1
null null 4 1
null null 5 1
this must be
stockholder_id election_id user_id user_type
null null 4 1
null null 5 1
this is my current not working code
select * from tbl_users
where not exists (select * from tbl_stockholders where election_id <> 2)

Try this:
SELECT * FROM users u
LEFT JOIN stockholders s ON u.user_id = s.user_id
WHERE u.user_type = 1 AND (s.election_id <> 1 OR s.election_id IS NULL)
Fiddle here.

I think, this is what you should do:
select
*
from
tbl_stockholders
left join tbl_users
on tbl_users.user_id = tbl_stockholders.user_id
where
(user_type=1
and election_id <> 1)
I don't think you intend to select null user_ids. Also that would invalidate the join i.e you are saying joined the two tables based user_id but select null user_id from the first table.

Related

How can I get the count of distinct userID with some condition

I want to query for each distinct user and their count which logged In as Yes
Userid loggedIn
---- -----
1 Yes
1 yes
1 No
2 Yes
2 No
3 No
4 yes
5 yes
1 No
Output Should be
Userid Count
---- -----
1 2
2 1
3 0
4 1
5 1
with main as (
select distinct Userid from <table>
)
select
main.Userid,
count(*) as total
from main
left join <table_name>
on main.Userid = <table_name>.Userid
and loggedIn = 'yes'
group by main.Userid
or
select
user_id,
sum(case when lower(loggedIn) = 'yes' then 1 end) as count_
from <table_name>
group by 1

Select rows which have no match in other table

This query seemed pretty simple to start with.
#table - user
user_id - full_name - image
1 Jon-1 Jon-placeholder.png
2 Aax-2 Max-placeholder.png
3 Lie-3 Lie-placeholder.png
4 Man-4 Man-placeholder.png
5 Nik-5 Nik-placeholder.png
6 Led-6 Led-placeholder.png
7 Neo-7 Neo-placeholder.png
#table - user_contacts
id - user_id - contacts_id - created_date
1 1 2 2020-02-26 08:43:26
2 1 3 2020-02-26 08:43:26
3 1 5 2020-02-26 08:43:26
4 3 4 2020-02-26 08:43:26
5 3 5 2020-02-26 08:43:26
6 7 2 2020-02-26 08:43:26
What i am trying to a accomplish is retrieve all contacts_id from user_contacts where the user_id is not equal to user_id = 1 which user Jon-1 so this must looks like
#retrieved data
user_id - full_name - image
4 Man-4 Man-placeholder.png
6 Led-6 Led-placeholder.png
7 Neo-7 Neo-placeholder.png
user_ids 6,7,4 must be retrieved because user_id = 1 does not have those ids in the user_contact table under column contacts_id
First Query
SELECT u.user_id, u.full_name, u.image
FROM user u
NATURAL LEFT JOIN user_contacts cnt
WHERE cnt.contacts_id IS NULL
Second Query
SELECT user.user_id, user.full_name, user.image
FROM USER
WHERE user.user_id NOT IN (
SELECT user_contacts.contacts_id
FROM user_contacts
WHERE user_contacts.contacts_id IS NOT NULL)
Third Query
SELECT u.user_id, u.full_name, u.image
FROM user u
NATURAL LEFT JOIN user_contacts cnt
WHERE cnt.contacts_id IS NULL
dbfiddle
SELECT
user_id, full_name, image
FROM
user
WHERE NOT user_id in (SELECT contacts_id from user_contacts WHERE user_contacts.user_id=1)
AND user_id <> 1
WHERE NOT user_id in sub-query: The sub-query will get the contact for user=1.
AND user_id <> 1: this makes sure user 1 is not selected.
With NOT EXISTS:
select u.* from user u
where user_id <> 1
and not exists (
select 1 from user_contacts
where user_id = 1 and contacts_id = u.user_id
)
See the demo.
Results:
| user_id | full_name | image |
| ------- | --------- | ------------------- |
| 4 | Man-4 | Man-placeholder.png |
| 6 | Led-6 | Led-placeholder.png |
| 7 | Neo-7 | Neo-placeholder.png |
SELECT a.*
FROM user a
LEFT
JOIN
( SELECT uc.*
FROM user u
JOIN user_contacts uc
ON uc.user_id = u.user_id
WHERE u.full_name = 'Jon-1'
) b
ON b.contacts_id = a.user_id
WHERE a.full_name <> 'Jon-1'
AND b.id IS NULL;

MySQL: How to add depended records to view

I have two tables:
goods
id parent_id name
1 null main_part
2 1 add_part1
3 1 add_part2
4 1 add_part3
orders
id good_id count
1 1 5
I want to get a view as :
orders_view
id good_id count
1 1 5
2 2 5
3 3 5
4 4 5
How to do in MySQL?
If your relation is a single level you could try joining the subquery for the union with the orders
select t1.id, t1.id good_id, o.count
from (
select id id_master, id
from goods
where parent_id is null
union all
select parent_id, id
where parent_id is not null
) t
inner join orders_view o on o.good_id = t1.id_master

How to get all value after where contain in sql

I get stuck in query of sql for this task
If i get all field after contain value 2 and ignore before value 2
For example like this
id profile_id status
1 1 3
2 1 3
3 1 2
4 1 1
5 1 1
and the result when i try like this, show all values where not contain 2
SELECT * FROM `users` WHERE `status` <> 2
id profile_id status
1 1 3
2 1 3
4 1 1
5 1 1
i try code subquery like this
SELECT *
FROM `users`
WHERE `users`.status != (
SELECT us.status
FROM `users` us
WHERE us.status = 2 LIMIT 1)
and the result show like this
id profile_id status
1 1 3
2 1 3
4 1 1
5 1 1
The result should i expected be like this, just show all field after the status = 2
id profile_id status
4 1 1
5 1 1
Thanks for your help
Try below query:
SELECT *
FROM `users`
WHERE id > (
SELECT id
FROM `users`
WHERE status = 2
)

How to write a query for selecting from three tables and if the values are null should not select?

I have three tables
1) Category_master
CID CTYPE
---------------------
1 2
--------------------
2 2
--------------------
3 1
-------------------
4 3
----------------
5 2
-------------------
6 3
2) NSS_maste
NID MID Name Value
1 5 Red 86
2 1 Blue 96
3) Sports_master
SID MID Name Cat
1 4 Walk Leg
The above is my table
I have to select CTYPE, NSS.NAME NSS.value,sports.name, sports.cat from category master,NSS_master and sports_master an it should not select null value Eg: for CID 2 there is no value in NSS_master table so it should not select but for CID 3 CTYPE 1 select name and other values as null
How to implement this thank you
Use Inner Join to avoid null values:
SELECT C.CTYPE, N.NAME, N.value, S.name, S.cat
FROM Category_Master C Inner Join NSS_master N ON C.CID = N.MID
Inner Join Sports_Master S ON C.CID = S.MID