MySQL Select ID where all columns under ID don't meet condition - mysql

I have a table "Payments" where student_id is a foreign key.
student_id Monthpaid year payment_id
51 1 2019 1
52 1 2019 2
51 2 2019 3
I tried this query:
SELECT * FROM Payments WHERE Monthpaid !=2
I get this result:
student_id Monthpaid year payment_id
51 1 2019 1
52 1 2019 2
51 2 2019 3
But I want this where ID 52 doesn't have Monthpaid = 2:
student_id Monthpaid year payment_id
51 1 2019 1

I think you ware looking for:
select t.*
from t
where not exists (select 1
from t t2
where t2.id = t.id and t2.monthpaid = 2
);
This returns all rows for an id where that id does not have a month of 2. That is how I interpret your question.

Related

mySql query higher then last user comment

I have the following table:
id post_id user_id to_user_id date time
---- ---------- -------- ------------ ------
1 100 1 2 10:00
2 100 1 2 10:30
3 100 2 2 11:00
4 100 5 2 11:30
5 100 8 2 11:45
6 105 10 50 09:00
7 105 2 50 09:30
8 105 11 50 11:00
9 105 30 50 11:30
10 105 32 50 11:45
On the following table you can see that user_id 2 has comments for post 100 and 105.
I need to get only the records per post_id that is hight than the first comment he wrote.
so for this example the result will be records 4 and 5 for post 100 and 8, 9, 10 for post 105 because 4, 5 is bigger than 3 (first record for user_id 2)
and 8, 9, 10 is bigger than 7 (user_2 first comment)
clear expected result:
id post_id user_id to_user_id date time
4 100 5 2 11:30
5 100 8 2 11:45
8 105 11 50 11:00
9 105 30 50 11:30
10 105 32 50 11:45
Could be with a subselect and an aggregation function
select * from my_table
where ( post_id, date_time) > (select post_id, max( date_time)
from my_table where user_id =2
group y post_id);
or if the tuple version donìt work properly try
select * from my_table as m
inner join (select post_id, max( date_time)
from my_table where user_id =2
group y post_id ) t on m.post_id = t.post_id
where m.date_time > t.date_time

count occurence of rows with specific status without using subquery

Here is my table
loan_id bid_id lender_id borrower_id amount interest duration loan_status
1 1 60 63 300.00 12.00 3 'completed'
2 2 61 63 300.00 12.00 3 'completed'
3 3 62 63 300.00 12.00 3 'pending',
4 1 62 63 300.00 12.00 3 'pending'
7 4 60 63 300.00 12.00 3 'completed'
I want to pull only those bid_id whose loan_status of all records is completed. It means if there is any record of bid_id with status pending then it will not pull that record.
I am using the followin query that is working fine:
SELECT bid_id
FROM loan
WHERE bid_id NOT IN (
SELECT l.bid_id
FROM loan l
WHERE l.`loan_status` = 'pending'
AND l.bid_id = bid_id
GROUP BY l.`bid_id`
HAVING COUNT(l.`bid_id`)>= 1
)
GROUP BY bid_id
Is there any other way in which we can get desired result without using sub query.
You can readily do this with group by and having:
select bid_id
from loan
group by bid_id
having sum(loand_status = 'pending') = 0

Improve sql query in MySQL in PHPMYADMIN

I have this query:
SELECT `jos_eb_registrants`. * , `jos_eb_field_values`. *
FROM jos_eb_registrants, jos_eb_field_values
WHERE `jos_eb_registrants`.`event_id` =3
AND `jos_eb_registrants`.`id` = `jos_eb_field_values`.`registrant_id`
It returns me everything from these two tables, but gives me rows with duplicate names, because ONE name from first table can have more than one field value. How to make field values not in separate rows, but in one row near one name, without duplicates. Please edit this query with my variables.Tables connected via Id and registrant_id.
jos_eb_field_values
1 id
2 registrant_id
3 field_id
4 field_value
jos_eb_registrants
1 id
2 event_id
3 user_id
4 group_id
5 first_name
6 last_name
7 organization
8 address
9 address2
10 city
11 state
12 country
13 zip
14 phone
15 fax
16 email
17 number_registrants
18 total_amount
19 discount_amount
20 amount
21 register_date
22 payment_date
23 payment_method
24 transaction_id
25 comment text
26 published
27 cart_id int(11)
28 deposit_amount
29 payment_status
30 coupon_id
31 check_coupon
32 tax_amount
33 registration_code
34 is_reminder_sent
35 is_group_billing
Try this:
SELECT DISTINCT jos_eb_registrants. * , jos_eb_field_values. *
FROM jos_eb_registrants
INNER JOIN jos_eb_field_values ON jos_eb_registrants.id = jos_eb_field_values.registrant_id
WHERE jos_eb_registrants.event_id =3

Merge multiple mysql rows having same id

I have a table like this:
id employee_id contract_id month year d1 d2 d3
1 25 1 11 2011 1 01 01
2 16 5 11 2011 1 11 0
3 29 3 11 2011 1 001 100
1 25 4 11 2011 0 11 011
Suppose I need data for month='11' AND year='2011', then for all rows having the same 'employee_id', the data should merge like this:
id employee_id contract_id month year d1 d2 d3
1 25 1,4 11 2011 1,0 01,11 01,011
2 16 5 11 2011 1 11 0
3 29 3 11 2011 1 001 100
I was trying GROUP_CONCAT but couldn't figure out the query. Please help.
SELECT
id,
employee_id,
GROUP_CONCAT(contract_id SEPARATOR ',') AS contract_ids,
`month`,
`year`,
GROUP_CONCAT(d1 SEPARATOR ',') AS d1s,
GROUP_CONCAT(d2 SEPARATOR ',') AS d2s,
GROUP_CONCAT(d3 SEPARATOR ',') AS d3s
FROM
`table`
WHERE
`month` = 11 AND `year` = 2011
GROUP BY
employee_id
SELECT *,
GROUP_CONCAT(`d2`) as `d2`,
GROUP_CONCAT(`d3`) as `d3`,
GROUP_CONCAT(`contract_id`) as contract_id
FROM table WHERE month='11' AND year='2011' GROUP BY employee_id;

MySQL Group By not producing expected result

This is my table structure:
rec_id product_id quantity quantity_in quantity_out balance stock_date status
1 2 342 NULL 17 325 2009-10-23 1
2 2 325 NULL 124 201 2009-10-23 1
3 1 156 NULL 45 111 2009-10-23 1
4 2 201 NULL 200 1 2009-10-23 1
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
All I want is the last transaction done for a given product: product_id, quantity, quantity_out and balance from this table.
Example, there are 2 transaction done for product 2 (ids 1 & 2):
final balance for product_id 2 is 0 -> stored in rec_id 5
final balance for product_id 1 is 76 -> stored in rec_id 6
Final result/output should be like this:
recid productid quantity quantityin quantityout balance stock_date status
5 2 1 NULL 1 0 2009-10-23 1
6 1 111 NULL 35 76 2009-10-23 1
You can find the latest record for each product like:
select max(rec_id) as MaxRec
from YourTable
group by product_id
Using a subquery, you can retrieve the latest rows for their product:
select *
from YourTable
where rec_id in (
select max(rec_id) as MaxRec
from YourTable
group by product_id
)
Here's a single query with no subqueries:
SELECT main.*
FROM YourTable main
LEFT JOIN YourTable newer
ON newer.product_id = main.product_id AND newer.rec_id > main.rec_id
WHERE newer.rec_id IS NULL;
You can tweak the field list however you want--make sure you select fields from main, not newer, which should be all null.