Two tables with all rows including allocated based on id - mysql

Im trying to make this generic as it might help others in the future.
For an example i have two tables one with books and the other is the user with which book they have read, So ide like to display all the books and include a temporary column value as a (yes / no or 0/1), i have tried a join but the ( WHERE user_id = 3) clause only then return the one row and not all the other rows.
book.book_id book.book_name
10 Book 1
11 Book 2
12 Book 3
-------------
user.user_id user.book_id
1 10
1 12
2 11
3 12
Desired output:
user_id book_id temp_col_read
3 10 0 // yes, on or null
3 12 1 // or yes
3 13 0

This is actually quite simple. In the event that a user could read a book multiple times, I would go with exists in the select:
select b.*,
(case when exists (select 1
from reads r
where r.book_id = b.book_id and r.user_id = 3
)
then 1 else 0
end) as user_read_book
from book b;
In MySQL, the case is not strictly necessary because a boolean expression is treated as 0/1 in many contexts:
select b.*,
(exists (select 1
from reads r
where r.book_id = b.book_id and r.user_id = 3
) as user_read_book
from book b;

You can use a left join and where the join is unresolved then is not read
select
user.user_id
, book.book_id
, case
when book.book_id is null
then 'NO' else 'YES'
end as temp_col_read
from book
left join user on user.book_id = book.book_id

Related

SQL count asset inventory

In sql help i have 3 tables, table one is asset table which is as follow
id
asset_code
asset_name
asset_group
asset_quantity
1
A001
computer
4
7
2
A002
keyboard
6
4
and another table is asset_allocation
id
asset_id
allocated_quantity
allocated_location
returned
1
1
2
IT office
no
2
2
1
main hall
yes
the last table is asset_liquidated which will present assets that are no longer going to be used
id
asset_id
liquidated_quantity
1
1
1
Now lets say that i have 7 computer out of which 2 are allocated but not returned and i have 4 keyboards out of which 1 is allocated and it is returned back and 1 computer is liquidated means it is never going to be used
so now here i want to join these 3 tables and find inventory of my current stock in hand.
Now this is the query now i need to add this
where asset_allocation.returned is enum no inside this query
SELECT id,asset_code, asset_name, asset_group, asset_quantity,allocated_quantity,liquidated_quantity,
asset_quantity - COALESCE(AA.allocated_quantity, 0) - COALESCE(AL.liquidated_quantity, 0) available_quantity
FROM asset A
LEFT JOIN (SELECT asset_id, SUM(allocated_quantity) allocated_quantity
FROM asset_allocation
GROUP BY asset_id) AA ON A.id = AA.asset_id
LEFT JOIN (SELECT asset_id, SUM(liquidated_quantity) liquidated_quantity
FROM asset_liquidated
GROUP BY asset_id) AL ON A.id = AL.asset_id;
I believe what you are looking for is adding WHERE returned = 'no' in your first JOIN like so:
SELECT id,asset_code, asset_name, asset_group, asset_quantity,allocated_quantity,liquidated_quantity,
asset_quantity - COALESCE(AA.allocated_quantity, 0) - COALESCE(AL.liquidated_quantity, 0) available_quantity
FROM asset A
LEFT JOIN (SELECT asset_id, SUM(allocated_quantity) allocated_quantity
FROM asset_allocation
WHERE returned = 'no'
GROUP BY asset_id) AA ON A.id = AA.asset_id
LEFT JOIN (SELECT asset_id, SUM(liquidated_quantity) liquidated_quantity
FROM asset_liquidated
GROUP BY asset_id) AL ON A.id = AL.asset_id;
That changes the available quantity for keyboard from 3 to 4 for me
your query:
vs. mine:

Not able to figure out sql query

i am new to sql and need help with a query. I have two tables user and user_family which contains data like
USER
ID
Date-Of-birth
User_Id
1
2021-05-21
28371
2
2021-04-17
28372
USER_FAMILY
ID
family_detail_id
User_Id
1
1
28371
2
1
28374
3
1
28375
4
2
28372
5
2
28373
6
2
28378
7
2
28379
i want to run a query which checks if current date in equal to someones dob in my user table, if yes i want to return all entries from user_family table which has same family_detail_id to someone whose dob has been matched.
Suppose if the current date is 2021-05-21 then the result should be,
ID
family_detail_id
User_Id
dob
birthday_user_id
2
1
28374
2021-05-21
28371
3
1
28375
2021-05-21
28371
You can use a self-join on the family_detail_id,
select f2.*
from user u join user_family f on f.id=u.id
join user_family f2 on f2.family_detail_id=f.family_detail_id
where u.Date_Of_birth=currdate();
Working Fiddle
I would suggest exists. Assuming that user.id matches to user_family.family_detail_id, then this looks like:
select uf.*
from user_family uf
where exists (select 1
from user u
where u.family_detail_id = u.id and
u.dob = curdate()
);
You may try below query, assuming USER_ID is the linking column between the two tables -
select uf1.*
from user_family uf1
where uf1.family_detail_id in
(select uf.family_detail_id
from user_family uf
inner join user u on u.user_id = uf.user_id
where u.Date-Of-birth = CURDATE()); --DATE(u.Date-Of-birth) = CURDATE()
HTH!

MySQL create a sum number if row exists in table

I have a statement like so:
select * from category a
inner join category b on a.row=b.relatedRow
inner join category c on b.row=c.relatedRow where a.row=?
I would like to get the number of "levels" like so:
If a has rows, level=1, If b has rows, level=2, If c has rows, level=3.
How can I do this?
Example
row, relatedRow
1,null
2,1
3,2
4,3
5,2
6,5
So, 1 is not related to any row, 2 is related to 1, 3 is related to 2 and so on...
If row=1, level 1 exists since 1 exists
level 2 exists since 2 is related to 1
level 3 exists since 3 and 5 is related to 2
level 4 exists since 6 is related to 5 and 4 is related to 3
Therefore the this tree goes down 4 levels.
try with something along the lines:
select
sum(case when not b.relatedRow is null then 1 else 0 end) as level1_total
sum(case when not c.relatedRow is null then 1 else 0 end) as level2_total
from category a
left join category b on a.row=b.relatedRow
left join category c on b.row=c.relatedRow where a.row=?
of course, you can modify the conditions in the case to suit your definition of has rows
select
max(
case
when c.relatedRow is not null then 3
when b.relatedRow is not null then 2
else 1
end
) as "levels"
from
A a
left outer join B b on b.relatedRow = a.row
left outer join C on c.relatedRow = b.row
Now seeing the edit to the question, I hope you see this pattern can be extended to a 4th level and beyond. If you add a where clause to do any filtering make sure that you only add conditions against A or you'll mess up the outer joins.

MySQL select rand and exclude users with condition

I need to select random user_id from "user" table, and completely exclude any user_id if current user have any "ongoing" battles with him battles.status
Query:
SELECT user.id
FROM user
LEFT JOIN battles b ON b.uid = user.id AND b.status <> 'ongoing'
WHERE user.id <> 1
ORDER BY RAND( )
LIMIT 1
But the query is not sufficient, because a user can have multiple battles with specific other users, one of them "ongoing" and the others "finished",
My query should select users from the "finished" row.
Tables structure:
user table:
id name
1 John
2 Sarah
3 Jack
4 Andy
5 Rio
battles table:
id uid uid2 status
1 1 2 finished
2 1 2 ongoing
3 2 3 ongoing
4 1 4 finished
5 3 5 finished
If "my" id = "1",
I want to completely exclude any user I have ongoing battle with him, like "2" in the above case and accept all other ids (i.e.3,4 and 5)
You probably want something along the lines of this:
SELECT foe.*
-- Select yourself and join all other users to find potential foes
FROM `user` AS me
INNER JOIN `user` AS foe
ON (me.id <> foe.id)
-- Here we select the active user
WHERE me.`id` = 1
-- Now we exclude foes we have ongoing battles with
-- (your id could be in either uid or uid2)
AND foe.`id` NOT IN (
SELECT `uid` FROM `battles`
WHERE `uid2` = me.`id` AND `status` = 'ongoing'
UNION ALL
SELECT `uid2` FROM `battles`
WHERE `uid` = me.`id` AND `status` = 'ongoing'
);
This will return a list of users which you do not currently have ongoing battles with. You can customise this to return just one of them using LIMIT and random ordering like in your example.

Counting votes and if user voted in MySQL table

I have 3 MySQL tables:
Features table:
id name
----------
1 feature 1
2 feature 2
3 feature 3
4 feature 4
5 feature 5
Votes table:
userid featureid
----------------
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
4 1
Users table:
id name
--------
1 John
2 Alice
3 Bob
4 Mark
5 Jane
6 Mary
7 Ann
I need to fetch in single query:
always all features, regardless if they have votes or not
each feature must be listed only once regardless of number of votes even if it has no votes
votes count for each feature listed
a special mark if currently logged user voted for current feature in the list - for example if user 3 is logged in, then list all features with votes count for all users and if user 3 voted for some features have a special field indicating his vote or NULL if he didn't (other data from votes table must be included too so it needs to be LEFT JOINED)
So far I did this:
SELECT *,(SELECT COUNT(*) FROM votes WHERE votes.featureid=features.id) AS "votecnt"
FROM features
LEFT JOIN votes ON votes.featureid=features.id
LEFT JOIN users ON users.id=votes.userid
GROUP BY features.id
It lists all features but has no special field if user 3 voted. I tried with IF, various WHERE conditions and after a lot of tries... ran out of ideas.
Desired output might look like this:
features.id features.name votes.userid votes.otherfields users.id users.name
1 feature 1 4 - 4 Mark
2 feature 2 NULL - NULL NULL
3 feature 3 NULL - NULL NULL
4 feature 4 NULL - NULL NULL
5 feature 5 NULL - NULL NULL
All the features are listed and only those where user 4 voted have other joined tables filled, others are simply NULL. If someone else voted for feature 2 it is still NULL as it is of no relevance for user 4 because in this example user 4 is logged in.
Here is the problem:
http://sqlfiddle.com/#!2/c3d10/3
http://sqlfiddle.com/#!2/c3d10/4
http://sqlfiddle.com/#!2/c3d10/5
http://sqlfiddle.com/#!2/c3d10/6
http://sqlfiddle.com/#!2/c3d10/7
All of above queries in SQLFiddle it should output all 5 features regardless of the userid. So the query must be modified somehow to show all features - even if other people voted for a feature or if there are no votes or if current user voted for feature.
this should do it:
SELECT tmp1.id, name, votecnt, user_id, user_name FROM
(SELECT *,(SELECT COUNT(*) FROM votes WHERE votes.featureid=features.id) AS "votecnt" FROM features) as tmp1
LEFT JOIN (SELECT features.id as feature_id, users.id as user_id, users.name as user_name FROM features
JOIN votes ON votes.featureid=features.id
JOIN users ON users.id=votes.userid
WHERE users.id=3) as tmp2 on tmp1.id = tmp2.feature_id
probably it's not the prettiest sql, and most likely there's also room for optimization
I think you're asking to have all features returned, all counts returned, but only user information if they voted for that feature.
I get the results you're looking for if you specify the userid
select f.id
, f.name
, u.id
, u.name
, v.votecnt
from features f
left join (select featureid, COUNT(userid) votecnt from votes group by featureid) v on v.featureid = f.id
left join votes v1 on v1.featureid = f.id and v1.userid = 4
left join users u on u.id = v1.userid
I chose to specify the userid inside the left join to the votes. Anywhere else and it limits the total number of rows returned.
Results:
1 feature 1 4 Mark 4
2 feature 2 NULL NULL 3
3 feature 3 NULL NULL 3
4 feature 4 NULL NULL 2
5 feature 5 NULL NULL NULL
Example with "Bob"
select f.id
, f.name
, u.id
, u.name
, v.votecnt
from features f
left join (select featureid, COUNT(userid) votecnt from votes group by featureid)v on v.featureid = f.id
left join votes v1 on v1.featureid = f.id and v1.userid = 3
left join users u on u.id = v1.userid
Results:
1 feature 1 3 Bob 4
2 feature 2 3 Bob 3
3 feature 3 3 Bob 3
4 feature 4 NULL NULL 2
5 feature 5 NULL NULL NULL