how i can count the repeated value in any field of mysql db's table.
Example:
id name
1 aaa
2 aaa
3 ttt
4 ccc
5 ttt
6 ccc
7 aaa
8 zzz
How i can get how many times value is repeated in table like.
aaa =3 times in table
ttt =2 times in table
ccc =2 times in table
zzz =1 times in table
I think it possible with count of the mysql but how to use it i dont know any one can help ?please answer ma question thanks in adv.
You need to group by your name column and then you can use an aggregate function like count() on that group
select name, count(id)
from your_table
group by name
Write below query to get count of each name
select name, count(name)
from your_table
group by name
OR
to get count of specific name
select name, count(name)
from your_table
where name = "aaa"
group by name
Related
here is my problem,
Imagine this table
id| idcompany
----------
1 | 1
----------
2 | 2
----------
3 | 1
----------
5 | 1
----------
6 | 2
I did a code in PHP like this:
if id <= 2 then DAY1
if id BETWEEN 3,4 then DAY2
if id BETWEEN 5,6 then DAY3
Looking to the table we can assume that company 1 appears on Day1, Day2 and Day 3
and company 2 appears Day1 and Day3
What I want to accomplish here is: How can I SELECT in SQL all the companies
who won't participate on Day 2?
I already tried:
SELECT * FROM tickets
WHERE id NOT BETWEEN 3 AND 4
GROUP BY(idcompany)
But its obvious that wont work because the Query will select the idcompany from another days and print on result.
Can someone help me to fig this out?
Thanks in advance.
Hope, I got you correctly
http://sqlfiddle.com/#!9/6f9921/1
SELECT idcompany
FROM tickets
GROUP BY idcompany
HAVING SUM(IF(id IN (3,4),1,0))=0
UPDATE If you need an interval you can replace condition in SUM:
SELECT idcompany
FROM tickets
GROUP BY idcompany
HAVING SUM(IF(id BETWEEN 330601 AND 40800,1,0))=0
To get a list of companies who didn't participate on day two, start by getting a list of those who did:
SELECT DISTINCT companyID
FROM myTable
WHERE id BETWEEN 3 AND 4;
Then you can exclude them from the final result by selecting all companies, and using a NOT IN operator to filter those out:
SELECT DISTINCT companyID
FROM myTable
WHERE companyId NOT IN(
SELECT DISTINCT companyID
FROM myTable
WHERE id BETWEEN 3 AND 4);
Here is an SQL Fiddle example.
Your table is rather confusing, I can't seem to find the information where Company 1 appears on Day 2.
But if I have understood correctly, here is a simple NOT or != operand for the WHERE operator example:
SELECT * FROM tickets
WHERE id != 3 AND id != 4
I have two tables in my database-
First Table Named area:
id name
1 a
2 b
3 c
Second Table Named data:
id name area_id end_date
1 a1 1,2 2014-07-14 09:50:00
I want to select rows from first table only if that id is not present in area_id set of my second table and whose end_date is greater than or equal to current date.
my query (not working) -
select a.* from area as a where a.id NOT IN (
SELECT find_in_Set(area_id) FROM data
WHERE date(end_date)>=CURRENT_DATE()
)
If I interpret your question right and the result of this query should be
id | name
---------
3 | c
then you could get your result with a little modification. You forgot the needle that should be found in the set:
SELECT a.*
FROM area as a
WHERE a.id NOT IN (
SELECT FIND_IN_SET(a.id, area_id) FROM data -- you forgot the needle
WHERE date(end_date) >= CURRENT_DATE()
)
But I would recommend to follow the good advice of juergen d and change the table structure, if you can.
I have two tables:
Table A: ID Items Data Pos
Table B: ID Apples Oranges Pos
And what I need to get all results from Table A and B ordered by position.
How can I do this? SELECT * FROM Table a and b orderby pos?
So for example the result should look like this:
Result from Table A with Id 1 Items 10 Data 5 and Pos 1
Result from Table B with Id 1 Apples 3 Oranges 3 and Pos 2
Result from Table B with Id 2 Items 4 Data 4 and Pos 3
Result from Table A with Id 2 Apples 7 Oranges 8 and Pos 4
Thank you.
You would use union all with an order by:
select ID, Items, Data, Pos
from tableA a
union all
select ID, Apples, Oranges, Pos
from tableB b
order by Pos
This is standard SQL so it will work in all the databases you mention.
If i have a MYSQL table that sorts names and one table that sorts value.
Example this is how the table look like:
ID name value
-- ------ -----
1 John 500
2 Rock 350
3 Wayne 700
4 John 350
5 Rock 250
6 Nick 100
7 Sweety 75
8 Lex 350
How do i display the total value for eache user? and also if i want to filter it to only show top 3 is there an easy command for that? Or do i need to make some kind of function.
In PHP
John 850
Wayne 700
Rock 600
Lex 350
Nick 100
Sweety 75
It can be accomplished by the aggregates, kind of
SELECT ID, name, SUM(value) as total_score
FROM user_scores
GROUP BY name
ORDER BY total_score
You need an aggregate function, specifically SUM. You can order a query by any value in the select list, and to go high-to-low just use the DESC keyword:
SELECT Name, SUM(value) AS TotalValue
FROM myTable
GROUP BY Name
ORDER BY TotalValue DESC
The AS TotalValue will ensure that the column name is TotalValue when you reference it in PHP.
how can I count Mysql Field Rows?
For example I have table called student_attendance where we have 4 fields: absent, present, holiday, leave.
There is list of 10+ students, each student value will go to its own field, like if for one student we selected absent, 1 value will go to absent field and if someone select present, 1 value will go to present field.
Now what i want is
Each time attendance take, new row insert in student_attendance table for the student.
So if there are 10 Rows in student_attendance table, how can i + all of them
Like if there are 10 rows of present field, 3 rows are empty and 7 rows have 1 value, how can i count it so that the total value of specific field 1+1+1+1+1+1+1 can comes in php so it show 7 ?
SELECT SUM(present) AS presence_days
FROM student_attendance
This will result:
+---------------+
| presence_days |
+---------------+
| 7 |
| |
+---------------+
Example SQL (Demo):
select count(*) from table_name where present_field=1;
Try this:
SELECT SUM(absent), SUM(present), SUM(holiday), SUM(leave)
FROM student_attendance
GROUP BY sudentId;