How to get data with condition SQL QUERY - mysql

So first of all, I have 2 table. The first one is the categories
Id Name
------------
1 Interior
2 Eksterior
3 Display
then the secon table is history which the data is the task I've finished
Id category_name category_id date (month) User Id
---------------------------------------------------------------
001 Interior 1 3 1084
002 Eksterior 2 3 1084
003 Interior 1 4 1089
004 Eksterior 2 4 1085
005 Display 3 4 1085
and what I want is to get categories by month, user id and know which one already done and not done from history, like this
example the data in March with user id 1084 :
Id Name Status
---------------------------
1 Interior done
2 Eksterior done
3 Display not done
or like this :
Id Name Status
--------------------------
1 Interior 1
2 Eksterior 1
3 Display 0
if the category in history table exist, the status will be 1 for done and 0 for not done.
this is my query before :
SELECT c.id, c.category, c.id=h.category_id status FROM categories c, history h WHERE MONTH(h.created_at)
I keep retrieving the wrong result for my query. Please help me..

Seems like:
SELECT *
FROM
categories c
LEFT JOIN history h on h.category_id = c.id AND h."date (month)" = 3
..will get you towards what you want: there will be NULL in the row from history table, for category Display; you can use this "is or is not null" to create your done/not done column

Related

Define table of choosed user row in multiple table list

I have control dashboard where multiple tables are listed with query
And in dashboard I can switch it to one table from ALLData to User1Table... and vice versa.
When there is only one table chosed I can easily manipulate data. However, I am struggling with updating rows when ALLData(all tables) are listed in dashboard. I can update it checking each table. I was wondering is there any better way to update it.
Tables have no DR. All tables have same column names.
//ALLData
SELECT * FROM users1
UNION ALL
SELECT * FROM users2...
user1
id name tel status
1 Bob 911 1
user2
id name tel status
3 Anna 11 0
3 Jack 12 1
//ALLData in dashboard
id name tel status
1 Bob 911 1
3 Anna 11 0
3 Jack 12 1
I can use id and status as PK

SQL Order by a specific value of a one to many field

The context :
I have these three simple SQL tables : my database diagram
a Request has one or N Request_Field
Request_Field is a pivot table
Value inside the pivot table Request_Field can sometimes contain dates in string format
a Field has 0 or N Request_Field
These tables are part of a greater database and I cannot change them at the moment.
My question :
Is it possible to query multiple "Request" and order them by a specific value in "Request_Field" ?
Example :
Request table
id
1
2
3
Request_Field table
id
request_id
field_id
value
1
1
1
John
2
1
2
2021-01-01
3
2
1
Jane
4
2
2
2020-01-01
5
3
1
Jack
6
3
2
2022-01-01
Field table
id
label
1
name
2
desired_date
The SQL query I try to make : get all Request ordered by the desired_date record of the Field table (DESC).
Request desired results
id
3
1
2
Thank you

mySQL - how to keep count() function from dropping 0 counts?

I'm new to mySQL and working on a query which essentially takes one initial table and summarizes its contents in two separate tables. The second "summary" table is dropping tuples where a value is 0; I need to fix that. Here's the specifics:
I have a large Inventory table, which I assemble from other tables:
SID CID SECTION RATING
==========================
100 1001 1 A
101 1001 1 A
102 1002 1 B
103 1002 2 F
104 1003 1 A
...etc...
("CID" and "Section" are the primary keys here, I don't know if whether the PK is spread across two attributes is significant here.) I have a successful query ("numTable1") which counts the number of SIDs for each CID and Section. For example, with the above Inventory table, "sumTable1" would be:
CID SECTION numSID
=====================
1001 1 2
1002 1 1
1002 2 1
1003 1 1
...etc...
Here's the SQL:
create temporary table numTable1(
SELECT ot1.cid, ot2.section, count(ot1.sid) 'numSID'
FROM otherTable1 ot1, otherTable2 ot2
WHERE ot1.cid=ot2.cid and ot1.section=ot2.section
GROUP BY ot1.cid, ot2.section
);
"NumTable" works great. Here's the problem: I also need a query which counts the number of 'A' Ratings per CID/Section:
create temporary table numA(
select t9.cid, t9.section, count(ot1.rating) 'As'
from otherTable1 ot1, t9
where grade = 'A' and t9.cid=ot1.cid and t9.section=ot1.section
group by t9.cid, t9.section
);
Here's the output of "numA":
CID SECTION As
=====================
1001 1 2
1003 1 1
...etc...
But here's the problem; some CID/Sections have no A Ratings, and they are being washed out. What I need is this:
CID SECTION As
=====================
1001 1 2
1002 1 0
1002 2 0
1003 1 1
...etc...
You'll note that the above table can be seamlessly merged with "numTable1", which is the ultimate aim here:
CID SECTION numSID As
==========================
1001 1 2 2
1002 1 1 0
1002 2 1 0
1003 1 1 1
But as long as "numA" drops CID/Sections with 0 As, I'm dead in the water. I assume my "group by" statement in "numA" is washing out the zeroes... but I'm not sure about that. Any suggestions?
#NigelRen provided the solution - a left join. Here's the code:
-- Left Join on numTable1 with numA... makes for total students // numAs
-- Resultant table is totA: "Total A Ratings"
create temporary table totA(
select numTable1.cid, numTable19.section, numTable1.numStudents, if(numA.As is null, 0, numA.As) 'A Ratings'
from numTable1
left join numA on numTable1.cid=numA.cid and numTable1.section=numA.section
);

Perform action on selected columns depending on their name

I've got a huge table, containing three "selection"-columns and many "data"-columns.
ID Thing1 Thing2 Thing3 avgData1 avgData2 highestEtc
---- -------- -------- -------- ---------- ---------- ------------
1 1 2 2 321 654 999
2 2 1 1 123 456 11
3 2 1 1 987 789 77
4 2 1 1 765 567 11
In my queries, I'm now selecting all entries with "Thing1" = x, "Thing2" = y, "Thing3" = z (Those three columns are selection-criteria.)
The purpose of getting those lines is to perform an action on each of the following data-columns: If it starts with "avg", I want to calculate an average of the specific column on all selected entries. On another prefix I want to count which number appears the most.
Is there a way of letting the MySQL Database do all this for me? I need a SQL-Statement that calculates the averages of the columns automatically, and performs other actions too.
For example, let's say I'd select the criteria Thing1=2, Thing2=1 and Thing3=1. Is there a way of writing the statement so that it returns only ONE entry, with the calculated things?
Result
----------------- ----------------- ----
(123+987+765)/3 (456+789+567)/3 11
I heard that this should be possible, and that it is a bad method of NOT letting the database perform those actions directly. Unfortunately, I have no idea how to do it.
Try this:-
SELECT ID, AVG(avgData1) AS RESULT1, AVG(avgData2) AS RESULT2, highestEtc
FROM YOUR_TAB
WHERE Thing1 = 2
AND Thing2 = 1
AND Thing3 = 1
GROUP BY ID
HAVING COUNT(highestEtc) > 1;
Hope this helps you.

MYSQL - Add multiple users to a table column

Maybe it's because I don't understand how to search for the right verbiage, but I'm having difficulty understanding how to attach multiple users to a table with multiple columns.
Here is what I'm attempting to do:
table name: user
user_id user_name
1 abc
2 xyz
3 pqr
4 new
table2 name : brackets
id user_id bracket_name
1 4,2 bracket_1
2 4,3,1 bracket_2
3 2,1 bracket_3
4 3,4,2 bracket_4
-- OR --
table name: user
user_id user_name brackets_id
1 abc 2,3
2 xyz 1,3,4
3 pqr 2,4
4 new 1,2,4
table2 name : brackets
brackets_id user_id bracket_name
1 4,2 bracket_1
2 4,3,1 bracket_2
3 2,1 bracket_3
4 3,4,2 bracket_4
I'm using nodejs and sequalize as my ORM and understand enough to read, write, delete and update to these tables, but when it comes to organizing my data, I'm completely lost!
Is it possible to add an array to MYSQL with the user ID's or the brackets that the user is allowed to access? The bracket are generated by a user and then they can invite their friends to join a bracket. Users can join multiple brackets and join other brackets as users.
Any guidance would be very helpful!
I think a Junction Table would simplify this for you: http://en.wikipedia.org/wiki/Junction_table
It would look something like:
table name: user
user_id user_name
1 abc
2 xyz
3 pqr
4 new
table2 name : brackets
brackets_id bracket_name
1 bracket_1
2 bracket_2
3 bracket_3
4 bracket_4
table3 your junction table:
user_id brackets_id
1 2
1 3
2 1
2 3
2 4
etc.etc.