find first row count and second row count using mysql - mysql

I have a table in mySql. I need to find how much entry in table which have entered only one time and another records which are enter for second time. please see the screenshot. count is based on shg_id.

if I correctly understand, you need this:
select entered, count(*) from (
select shg_id, count(*) as entered
FROM mytable
group by shg_id
having count(*) between 1 and 2
)t
group by entered

The following should do if it has an id attribute:
SELECT * FROM Table
HAVING COUNT(shg_id) = 1 -- Record equal to 1
Or
SELECT * FROM Table
HAVING COUNT(shg_id) = 2 -- Record equal to 2
Updated - This works well on my side:
SELECT COUNT(shg_id) AS Total
FROM Table
WHERE shg_id= 4
GROUP BY shg_idHAVING COUNT(shg_id) = 1
Another one - Slightly taken from OTARIKI:
SELECT shg_id, COUNT(*) AS Total FROM Table
GROUP BY shg_id
HAVING COUNT(shg_id) BETWEEN 1 and 2

Related

Multiple selection of ID's (MySQL)

Is it possible for me to select multiple ID? For example: I am executing this command
SELECT
sum(amount)
FROM
bets_logs
where
fight_id=1
Assumming that I have id that is up to 500 Of course I'll do it one by one... My target is, is there a query that I can select id from 1 to 10. So that I won't do it 1 by one.
My target is, is there a query that I can select id from 1 to 10. So
that I won't do it 1 by one.
You can write the query like following.
SELECT fight_id, sum(amount)
FROM
bets_logs
where
fight_id >=1 and fight_id<=10
group by fight_id

count of diferent values with the same id

I have ID_employe in one column (the same ID can be on more than 2 rows) and in other column I have ID_job.
I need MYSQL to find same values in the first column and then check, if there are everytime the same values in the second column.
If there is any difference, I need to give me number of ID_employe what has it different.
So example:
from this example I need SQL to give me result: 2
(because ID_employe 1 and 3 has different ID_job)
Thank you very much!
With EXISTS:
select count(distinct t.ID_employe) counter
from tablename t
where exists (select 1 from tablename where ID_employe = t.ID_employe and ID_job <> t.ID_job)
You can use a having clause and compare the minimum and maximum id_job per id_employee to exhibit those that have at least two jobs. Then you can count in another level of aggregation:
select count(*) cnt
from (
select id_employee
from mytable
group by id_employee
having min(id_job) <> max(id_job)
) t

Mysql how to group by multiple columns, and select a flag value based on which group matched

Suppose I want to take all duplicate values of a table by following matched criteria's:
1- matched by (first_name,last_name,father_name).
2- matched by (nationalId).
3- matched by (phone_number).
Suppose I want to get all users whose registered them self more than one using above conditions.
Now i want to take those duplicate records, along the other columns a i need to select a a column matched_type that should has one of these 1,2,3 values based on what matched occurred.
Now i have separate query for each matching criteria, is it possible to do it with one query?
I have these queries:
SELECT u.first_name,u.last_name,u.father_name,u.national_id,u.phone,COUNT(u.id) AS total,'1' AS match_type
FROM users
GROUP BY CONCAT(u.first_name,u.last_name,u.father_name)
HAVING total > 1
and for second matching criteria:
SELECT u.first_name,u.last_name,u.father_name,u.national_id,u.phone,COUNT(u.id) AS total, '2' AS match_type
FROM users
GROUP BY u.national_id
HAVING total > 1
and for the last one:
SELECT u.first_name,u.last_name,u.father_name,u.national_id,u.phone,COUNT(u.id) AS total, '3' AS match_type
FROM users
GROUP BY u.phone
HAVING total > 1
And then i have like this:
SELECT src.* FROM (first_query UNION ALL second_query UNION ALL third_query)
Pls check is this ok?
SELECT first_name,last_name, father_name, nationalId, phone_number ,CASE
WHEN first_name=[Value] AND last_name=[Value] AND father_name = [VALUE] THEN 1
WHEN nationalId=[VALUE] THEN 2
WHEN phone_number=[VALUE] THEN 3
ELSE 0 END as matched_type FROM [TABLE_NAME] WHERE
(first_name=[Value] AND last_name=[Value] AND father_name = [VALUE])
OR nationalId=[VALUE] OR phone_number=[VALUE]

Multiple rows come back of same ID, just need one

I am trying to list several products on a page. My query returns multiples of the same product and I am trying to figure out how to limit it to one only with my query.
The primary key on the first table that we will call table_one is ID.
The second table has a column of ProductID that references the primary key on table_one.
My query brings me back multiples of my ProductID that is equal to 6 below. I just want one result to be brought back, BUT I still want my all of my data in DateReserved on table_two to be queried. Pretty sure I need to add one more thing to my query, but I have not had much luck.
The results I want back are as follows.
ID Productname Quantity Image Date Reserved SumQuantity
6 productOne 6 'image.jpg' 03-31-2013 3
7 productTwo 1 'product.jpg' 04-04-2013 1
Here is my first table. table_one
ID Productname Quantity Image
6 productOne 6 'image.jpg'
7 productTwo 1 'product.jpg'
Here is my second table. table_two
ID ProductID DateReserved QuantityReserved
1 6 03-31-2013 3
2 6 04-07-2013 2
3 7 04-04-2013 1
Here is my query that I am trying to use.
SELECT *
FROM `table_one`
LEFT JOIN `table_two`
ON `table_one`.`ID` = `table_two`.`ProductID`
WHERE `table_one`.`Quantity` > 0
OR `table_two`.`DateReserved` + INTERVAL 5 DAY <= '2013-03-27'
ORDER BY ProductName
Sorry for posting another answer, but as it seems my first try on it was not so good ;)
To only get one result row per reservation you need to sum them up somehow.
First I suggest you explicitly select the columns you want back in your result and don't use "*".
I suggest you try something like this:
SELECT
`table_one`.`ID`, `table_one`.`Productname`, `table_one`.`Image`, `table_one`.`Quantity`,
`table_two`.`ProductID`, SUM(`table_two`.`QuantityReserved`)
FROM
`table_one`
LEFT JOIN
`table_two` ON `table_one`.`ID` = `table_two`.`ProductID`
WHERE
`table_one`.`Quantity` > 0
OR `table_two`.`DateReserved` + INTERVAL 5 DAY <= '2013-03-27'
GROUP BY `table_two`.`ProductID`
ORDER BY ProductName
As you see I used "SUM" to get a combined quantity, this is called aggregation and the "GROUP BY" helps you getting rid of multiple occurences of the same ProductID.
One problem that you have now is that you will have to get the reservation date from a seperate query (well at least I am now unsure how you would get it into the same query)
Since you are using MySQL
LIMIT <NUMBER>
should exactly do what you want, you just insert it after your ORDER BY clause, but probably you should also add one more ordering to that, so you can be sure that you will always get the one entity that you wanted and not just some "random" entity ;)
So without further ordering your query would look like this:
SELECT
*
FROM `table_one`
LEFT JOIN `table_two` ON `table_one`.`ID` = `table_two`.`ProductID`
WHERE
`table_one`.`Quantity` > 0
OR `table_two`.`DateReserved` + INTERVAL 5 DAY <= '2013-03-27'
ORDER BY ProductName
LIMIT 1
here some more description about that
SELECT a.member_id,a.member_name,a.gender,a.amount,b.trip_id,b.location
FROM tbl_member a
LEFT JOIN (SELECT trip_id, MAX(amount) as amount FROM tbl_member GROUP BY trip_id ) b ON a.trip_id= b.trip_id
LEFT JOIN tbl_trip b ON a.trip_id=c.trip_id
ORDER BY member_name

MYSQL: Count the number of times a specific integer appears in a column then making a count of how many of these are present?

I have a table with figures like this
Report used UserID
1 2
1 2
1 2
2 2
In this case I'm looking to count the 1's in the 'Report used' column, which would give me the value 3. I might find a few of these in this column for different users, so I'd want to count how many times I found 3 1's.
I've tried using SELECT COUNT to count specific numbers but I'm not sure how to count this count, if you follow me.
Try this:
SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid
Also check this:
SELECT COUNT(userid)
FROM (SELECT userid, COUNT(reportused) onescount
FROM tablename
WHERE reportused = 1
GROUP BY userid) a
WHERE onescount = 3
If I've got it right:
select Report_used,RU_count,count(*)
from
(select Report_used, UserID, count(*) RU_Count
from t
group by Report_used, UserID) t1
group by Report_used,RU_count;