help with mysql select query - mysql

what I'm trying to do:
from these tables
---------------------------------
name date state
---------------------------------
ali jan 12 started
ali jan 12 drop out
masa jan 12 registered
masa jan 12 started
sami jan 12 started
I want the results to be
---------------------------------
name date state
---------------------------------
masa jan 12 started
sami jan 12 started
So basically what i want is to have all the started users without the ones who dropped out
so the filtering should be based on the state
thanks

Those two rows in your result example are not the only started people on that date.
SELECT * FROM table WHERE state = 'started';
Would return 3 rows:
---------------------------------
name date state
---------------------------------
ali jan 12 started
masa jan 12 started
sami jan 12 started
To get the two rows in your example you need:
SELECT * FROM table WHERE name IN ('sami', 'mesa');
Update, added this example
You could limit if you only wanted two rows:
SELECT * FROM table WHERE state = 'started' LIMIT 2;

Perhaps something along the following lines is what you need...
SELECT NAME,
DATE,
STATE
FROM MYTAB
WHERE STATE = 'Started' AND
NOT EXISTS (SELECT *
FROM MYTAB MYTAB2
WHERE MYTAB2.NAME = MYTAB.NAME AND
MYTAB2.STATE = 'Drop Out');
That should get everyone who started and didn't drop out.

SELECT name, date, state FROM table GROUP BY name HAVING state = 'started';
I believe this would eliminate people who dropped out eventually because the GROUP BY would put the people with the same name into the same group, and then eliminate them once they drop out with the HAVING statement.

select * from table where state != 'dropped out'

Related

Select from database where there aren't any records since the last three months

I need to select the defaulting companies from my table (those who doesn't register a payment since october)
This is an example of what my table looks like:
id
company_id
deposit_date
year
month
1
578
2021-10-12
2021
10
2
254
2021-11-17
2021
11
3
465
2021-12-15
2021
12
4
159
2022-01-12
2022
1
I must use month as reference and not deposit_date
Any help will be much appreciated. Thanks in advance.
Check for those where last payment prior to nov 2021
drop table if exists t;
create table t(id int,company_id int,deposit_date date,year int,month int);
insert into t values
(1 ,578 ,'2021-10-12' ,2021 ,10),
(2 ,254 ,'2021-11-17' ,2021 ,11),
(3 ,465 ,'2021-12-15' ,2021 ,12),
(4 ,159 ,'2022-01-12' ,2022 ,1);
select company_id ,year*100+month
from t
group by company_id
having max(year*100+month) < 202111;
You can use select and where clause to filter the data

Sort values in two column and insert order position into another column in mysql

I have a database about sports event that contains:
*User ID
*Amount of Points that the user got on that event
*Time (HH:MM:SS) that took the user to complete track.
How can I first sort them by no. of points, then if two users have same amount of points, by time (shorter is better); and then insert the places to rows?
I have database like that:
ID No. of Points Time Place
------------------------------------
1 15 00:56:00
2 13 00:55:15
3 17 01:00:00
4 17 00:57:00
5 19 00:52:15
I need to have it with places:
ID No. of Points Time Place
------------------------------------
1 15 00:56:00 4
2 13 00:55:15 5
3 17 01:00:00 3
4 17 00:57:00 2
5 19 00:52:15 1
I hope, you understand that. Sorry for bad English.
Best regards,
You can do this with update statement as follows.
SET #placeValue:=0;
UPDATE [Table Name] SET Place=#placeValue:=#placeValue+1 ORDER BY
[Amount of Points] DESC,Time ASC

Vlookup similar in mysql

Let's say I have the table ABC
RENT
- 3 5 6 7 9 10
MONTH
- Jan Mar Jan Jul Dec Feb
How would I go and select the MONTH corresponding to the Minimum Rent?
This is basically performing a MIN operation on the RENT but then I'm completely unaware of how to relate it to the MONTH column and extract the correspondent value.
Can you help?
Supposing you mean a table ABC like this
MONTH RENT
----- ----
Jan 3
Mar 5
Jan 6
Jul 7
Dec 9
Feb 10
Then your SELECT to get minimum rent would be
SELECT MONTH FROM ABC WHERE RENT=(SELECT MIN(RENT) FROM ABC);
When working my MySQL, you need to think about relationships in your database design. Is the month to rent relationship a 1-to-1, 1-to-many, many-to-1, many-to-many?
If you have a relationship that isn't 1-to-1, the best way to implement if is to have 3 tables. A MONTH table, a RENT table, and a table that correlates them.
TABLE_NAME column1 column2
MONTH pkid month
RENT pkid amount
MONTHLY RENT pkid fk_month fk_rent
From here, you can just do a join on the three tables using the correct columns to get the answer you want.

Counting rows from Where Clause and display every count in a different column

Hi I have a MySql Query as follows:
Select Count(*),status,time from table where Status in (2,3,8) group by status,time
The Output is ok i get something like
Count Status time
10 - 1 - 2014
10 - 2 - 2014
19 - 1 - 2015
11 - 2 - 2015
But what i would like to have is something like
Count of Status1 - Count of Status2 - Time
10 - 10 - 2014
19 - 11 - 2015
Is there a nice and efficient way to do since since my database has about 1 mio records. I want to use this data to show it in a table without further manipulation.
Thanks in advance
The following should work:
SELECT SUM(IF(`Status` = 1,1,0) ) CountOfStatus1,SUM(IF(`Status` = 2,1,0) ) CountOfStatus2,TIME FROM TABLE WHERE STATUS IN (1,2) GROUP BY TIME
You will need to adjust it for whatever statuses you want to see.

Select distinct user_id from a table for each request type

I have a table with 4 columns:
ID, USER_ID, SOURCE, CREATED_DATE
In that table is the following data:
ID USER_ID SOURCE CREATED_DATE
1 25 PURCHASE 2012-01-01 12:30:00
2 26 PLEDGE 2012-01-01 12:40:00
3 25 PLEDGE 2012-01-01 12:50:00
4 25 PURCHASE 2012-01-14 12:00:00
Now as you can see, I have 4 rows of data, and two unique users. User (25) made 3 transactions (two purchases and one pledge), user (26) made one transaction – (one pledge)
Here is what I am trying to achieve:
I need to select ALL transactions from this table, but I want to select a UNIQUE user for each REQUEST TYPE (source), and that row needs to be the EARLIEST TRANSACTION.
My expected result data would be:
ID USER_ID SOURCE CREATED_DATE
1 25 PURCHASE 2012-01-01 12:30:00
2 26 PLEDGE 2012-01-01 12:40:00
3 25 PLEDGE 2012-01-01 12:00:00
User (25) made TWO PURCHASES (one on 2012-01-01 and one on 2012-01-14) – the first is the one that gets returned.
This is the SQL I have come up with so far:
SELECT
Supporter.user_id,
MIN(Supporter.created) as created,
Supporter.*,
Supporter.source
FROM
supporters AS Supporter
GROUP BY Supporter.source
ORDER BY Supporter.created ASC
Now, this gets me really close, except it only selects ONE of the user id’s (the one with two items – a pledge and a purchase). If I could figure out how to select the data on both users, that would be what I need to do! Can anyone see what I am possibly doing wrong here, or missing?
You need to group by source and by user id
Something like this
SELECT
Supporter.user_id,
MIN(Supporter.created) as created,
Supporter.*,
Supporter.source
FROM
supporters AS Supporter
GROUP BY Supporter.user_id, Supporter.source
ORDER BY Supporter.created ASC