I want to calculate how much some names repeated and to get he most repeated name out but cannot get it. I can calculate how much Michael has $fix in his rows. But I need who is the best of it in repeats.
SELECT COUNT(*) AS count FROM (SELECT * FROM names WHERE league='books' and $position='Michael' ORDER BY id LIMIT $limit) AS last12 WHERE $fix='1'
I want to print me Michael if he repeats the most:
Michael 1
Jack 1
Jack 1
Jack 1
Michael 1
Michael 1
Michael 1
Juni 1
Let's say you have a table called names with columns id, name, league, etc. You want to know how many Michael or Erick or whatever name are there. To do that, you need to group by that column and use count(*), like follows:
Select name, count(*) as count from names group by name
This will return the names with its respective counts.
Related
i am trying to find duplicate entries within my mysql table. I would like to compare the different fields with each other. Here is the structure of my table:
ID FirstName LastName Street ZIP City IpAddress
1 Jack Smith 2nd 12345 Sample1 12.21.24.212
2 Paul Miller 3rd 45685 Sample2 78.54.85.654
3 Jenny Smith 3rd 77273 Sample3 84.91.67.311
4 Frank Jackson 1st 27819 Sample1 78.54.85.654
5 Jack Smith 3rd 72891 Sample2 94.79.99.465
Now i would like to compare the street and ip column individually and then i would like to find the combination of the first- and lastname. There are actually a few more columns in my table that i would like to search for but i think my example above should give you an idea about what i am planning.
I need the id numbers of the entries that could potencially duplicates.
In the example above the output should be the id numbers 1 and 5 when i compare the combination of the first- and lastname.
The output should be the id numbers 2,3 and 5 if i compare the street names.
And the output for the ip addresses should be id numbers 2 and 4.
Does anyone have some ideas about how i should do this? What is the best way to compare those different tables? I don't mind if i have to do several queries.
Use GROUP_CONCAT() to get all the IDs within a group, and GROUP BY to specify the columns that you're looking for duplidates of. And you can use COUNT(*) so you only return the ones that have duplicates.
For streets:
SELECT street, GROUP_CONCAT(id)
FROM yourTable
GROUP BY street
HAVING COUNT(*) > 1
For names:
SELECT firstname, lastname, GROUP_CONCAT(id)
FROM yourTable
GROUP BY firstname, lastname
HAVING COUNT(*) > 1
I have two columns in a MySQL table, one called "Total Experience" and one called "id"
Is there a way to add up the value of Total Experience and put it in a variable for all users with the same id?
Example: There are 3 users: Steve, Jack, and Sam
Steve has the id of 1 and Total Experience of 500
Jack has the id of 1 and Total Experience of 400
Sam has the id of 2 and Total Experience of 700
Is there a way to select and add up Steve and Jack's Total Experience in a SQL query, as they have the same id?
Use GROUP BY to aggregate rows by a column.
SELECT id, SUM(`Total Experience`) AS experience
FROM YourTable
GROUP BY id
Hi I got an search engine where users can search for different names.
The name of the table is "searched_names" and I wanna echo out the first 6 names ordered by most searched. There can example be ten people named Alex in the table and 8 John and 1 Peter then I would like it to echo it out like this.
Alex
John
Peter
SELECT * FROM names WHERE ... guess I'll use COUNT() somewhere
Use ORDER BY and LIMIT
SELECT *
FROM names
WHERE ...
GROUP BY name
ORDER BY COUNT(*) DESC
LIMIT 6
he wants ordered by most searched.
SELECT * FROM names
ORDER BY col_containing_nb_of_search
limit 6
Say I have a database with two tables: "food", and "whatToEat".
I query the "whatToEat" and find 3 rows:
id Food username
1 Apple John
2 Banana John
3 Milk Linda
If I want to get those from the "food" table, I can just do something like this i guess:
SELECT *
FROM food
WHERE username='John' AND typeOfFood = 'apple'
OR typeOfFood = 'Banana' OR typeOfFood = 'Milk'
... but is it possible to dynamically write this, since the "whatToEat" table will change all the time, or do I need a loop and query the "food" table one by one for each of the objects in "whatToEat"?
EDIT
The above is just an example, the real scenario is an online game. When it's a players turn in a game, he's put on the "matches_updated" table. This table just holds his name, and the id of the match (or matches since he can be in several at the same time). When a player recive an update, I would like to check if he have any matches that needs to be updated (query "matches_updated" table), and then pull the data and return to him from the "matches" table, where all the information is stored about the matches.
Example:
The player Tim query the "mathces_updated" table and find he have 2 new matches that needs to be updated:
match_id username
1 Tim
2 Tim
2 Lisa
1 John
3 John
... He now want to get the information about these matches, which is stored in the "matches" table:
match_id match_status player1Name Player1Score Player2Name Player2Score
1 1 John 123 Tim 12
2 1 Lisa 4 Tim 15
3 1 John 0 Lisa 0
I am not sure whether I understand the question correctly.
Actually It depends on the queried tables have what in common.
so if suppose food is a common column, then query something like this...
select * from food where food in (select food from whattoeat where username = ?)
Try it out, if it solves your problem...
SELECT * FROM matches_updated JOIN matches
ON matches_updated.match_id == matches.match_id
WHERE matches_updated.user == "Tim"
--
Perhaps you want a JOIN statement?
SELECT * FROM food JOIN whattoeat
ON food.username == whattoeat.username
WHERE food.username == "John"
I'm having a really hard time trying to understand what your desired result is - posting example of both tables in question, and the desired result of your query, might help.
SELECT * FROM food WHERE username='John'
for example i have a table like this :
name rating
matei 124
andrei 20
serj 25
john 190
mike 96
andy 245
tom 73
i need to output something like this(order by rating):
john's position is 2; or, tom's position is 5; (i don't need to get all result , just one )
How can I achieve this?
Thanks in advance
Generally order of rows in a query result is not guaranteed by MySQL unless ordering is explicitly specified with ORDER BY clause. If you have some separate ordering column, you may use query like the following:
SELECT count(1) as position
FROM table
WHERE order_column <= {john's order_column value};
If you don't have ordering column, I'd recommend you to define first, what does "john's position" and "tom's position" mean.
UPDATE:
AFAIU, you want to get position in list sorted by rating (sorry, I initially did not get it). So, rating would be your order_column. In this case, you should decide, how do you calculate position, if two guys have equal rating (who's position is higher?).
So, the query may look in the following way:
SELECT count(1) as position
FROM table
WHERE
rating > (SELECT rating FROM table WHERE id={user's ID});
SELECT COUNT(*) + 1
FROM users
WHERE (rating, name) <
(
SELECT rating, name
FROM users
WHERE name = 'john'
)
Note that if you will have duplicates on both name and rating, this query will assign the same rating to both of them.
Tables are more formally known as relations in database literature - they are not guaranteed to be ordered (they are sets of "tuples"), so your question doesn't make sense. If you need to rely on an order/position, you need to define an additional column (like an auto-incrementing ID column) to capture and store that info.
Is this any help > http://craftycodeblog.com/2010/09/13/rownum-simulation-with-mysql/ ?
Would offset not work like so?
SELECT * FROM Table ORDER BY rating DESC LIMIT 1,6
This would return 1 row that has been off setted by 6 rows ? or am I mistaken, the syntax would be
SELECT * FROM Table ORDER BY rating DESC LIMIT 1 , {{POS}}