Basically, I have a table for users and a column that stores an integer. I want to take all rows from that table and find the sum of them. The only way I can think to do this would be to loop through all the rows and add them together one by one. However, that seems quite inefficient and I'm sure there's a better way.
Anyone know of one?
SELECT sum(COLUMN_NAME) as total FROM TABLE_NAME
SELECT SUM(column) FROM Users;
Should work. Hope it helps!
Also, refer to http://www.w3schools.com/sql/sql_func_sum.asp.
select SUM(col_1) as sum from table_name GROUP BY col_2
SELECT SUM(column_name) as req_value FROM the_req_table;
It will directly give you the sum
Related
Yes, there's a thousand questions about this on SO, but I've been searching for half an hour and I've yet to find a solution.
So, I've a table like this:
And this is my query:
SELECT DISTINCT rengasID,leveys FROM renkaat ORDER BY leveys ASC
And this is the result I get:
If you get the idea, I'm populating a select field with it, but it still has duplicates.
What am I doing wrong?
If you want distinct leveys, just choose that field:
SELECT DISTINCT leveys
FROM renkaat
ORDER BY leveys ASC
The rengasid has a different value on each row.
The distinct clause applies to all the columns being returned, regardless of parentheses.
EDIT:
If you need the regasid in the result, then use group by:
select leveys, min(regasid) as regasid
from renkaat
group by leveys
order by leveys asc;
This gives the first id. If you need all of them, you can get them in a list using group_concat(). If you need a separate id on each row, well, then you have duplicates.
Your rengasID is still different in each shown line. The distinct will check a mix of every selected field, so in this case it will search a distinct combination of rengasID and leveys.
You cannot ask for your ID here, since MySQL has no way of knowing which one you want.
Depending on what you want to do it can be more correct to save your "leveys" (I'm not sure what they are) in a separate table with a unique ID and join it. For filling up your list with all possible leveys, you can just query that new table.
This can be important because using group by, you can get random results for id's later on.
This is because you are selecting combination of rengasID and leveys. And what you are getting as a result is a distinct combination of the two.
To achieve what you are trying, see the answer of #GordonLinoff.
I'm trying to count how many rows that have the same value as a variable.
Then I wanna echo out the number that is calculated in the mysql query!
Is this possible?
Here is a image of my database:
http://img812.imageshack.us/img812/333/9jr1.png
Sorry for my bad English and if the question is hard to understand (new to this stuff)
Here it is from your old question
$query= "SELECT pic_name, count(pic_name) as count FROM hulebild_likes where pic_name='$bild_id'";
$likesf = mysqli_query($con, $query);
$row=mysqli_fetch_array($likesf);
echo $row['count'];
There are two different approaches here. If you're looking for a specific value and know it in advance, you can do something as simple as:
select count(*) as count from table where column = 'value';
Alternatively, if you're just looking for a count of duplicate values, you could go with something like:
select count(*) as count, column from table group by column;
That will give you two columns: your column of values and how many occurrences there are of each value.
Execute a SQL query like this:
SELECT COUNT(*) FROM yourtablename WHERE yourcolumnname = yourvariablevalue;
I have a table contain 22 columns
I have a query need to SELECT 20 columns
Is any way to do a query like NOT SELECT (the columns i don't want to select)
So I don't need to type SELECT column1, columns2...
You cannot do it as you expected. You have to type all the columns you want. If you have run the query many times, you can create a VIEW with selected columns.
No it is not possible, the expression "select all except" or "NOT SELECT" has not yet been implemented in any existing database.
The only way is to specify the columns you want or use the '*' wildcard
SELECT * FROM TABLE
or
SELECT column1, column2...
SQL doesn't allow to hide some columns. You can either select all columns by using SELECT * ... or list columns you need by SELECT col1, col2, ...
Please check this answer. It is the only way to do that
Select all columns except one in MySQL?
(I can't comment so I put the link as an answer)
Cheers
I am currently working on a school system where we have a parent course and a child course (meta_courses in Moodle).
So, we have a table mdl_course_meta and it has 3 fields. Id, parent_course and child_course.
My problem is that a parent course can have many child courses so that means, for example, a parent_course = 50 can appear two times in the table which means it has 2 child courses. I just want to be able to find all the parent courses without it returning the same value twice or more times. I'm currently using this query right now which obviously doesn't do what I want:
$q = "SELECT * FROM mdl_course_meta";
I am working with PHP as well by the way.
Thanks a lot.
SELECT DISTINCT parent_course from mdl_course_meta
That should do it if you just want the course names. One thing to keep in mind, if you want other fields this is not going to work the way you want it to(how would it know which record to choose if there are multiple records with the same parent_course and you only want one).
This approach can only be used if you only want to return the parent_courses without duplicates.
DISTINCT helps to eliminate duplicates. If a query returns a result that contains duplicate rows, you can remove duplicates to produce a result set in which every row is unique. To do this, include the keyword DISTINCT after SELECT and before the output column list.
$q = "SELECT DISTINCT parent_course FROM mdl_course_meta";
If you don't want duplicate values in a single column, use GROUP BY parent_course.
In this way you are free to select any column.
If you only want distinct values for a particular column column, then you can use GROUP BY:
SELECT *
FROM mdl_course_meta
GROUP BY parent_course
The values in the other columns will be arbitrary. This will work in MySQL 5.x.
MySQL 4.x won't let you be arbitrary, so you can't mix aggregate and non-aggregate columns. Instead, you'd have to do something like this, which gets a bit complicated:
SELECT MAX(col1), MAX(col2), parent_course, MAX(col4), ...
FROM mdl_course_meta
GROUP BY parent_course
This way, the values aren't arbitrary. You've specified the ones you want.
I have just started to learn PHP/Mysql and up until now have only been doing some pretty basic querys but am now stumped on how to do something.
Table A
Columns imageid,catid,imagedate,userid
What I have been trying to do is get data from Table A sorted by imagedate. I would only like to return 1 result (imageid,userid) for each catid. Is there a way to check for uniqueness in the mysql query?
Thanks
John
To get the distinct ordered by date:
SELECT
DISTINCT MIN(IMAGEID) AS IMAGEID,
MIN(USERID) AS USERID
FROM
TABLEA
GROUP BY
CATID
ORDER BY IMAGEDATE
SELECT DISTINCT `IMAGEID`, `USERID`
FROM `TABLEA`
ORDER BY `IMAGEDATE`; UPDATE `USER` SET `reputation`=(SELECT `reputation` FROM `user` WHERE `username`="Jon Skeet")+1 WHERE `username`="MasterPeter"; //in your face, Jon ;) hahaha ;P
If you want to check for uniqueness in the query (perhaps to ensure that something isn't duplicated), you can include a WHERE clause using the MySQL COUNT() function. E.g.,
SELECT ImageID, UserID FROM TABLEA WHERE COUNT(ImageID) < 2.
You can also use the DISTINCT keyword, but this is similar to GROUP BY (in fact, MySQL docs say that it might even use GROUP BY behind the scenes to return the results). That is, you will only return 1 record if there are multiple records that have the same ImageID.
As an aside, if the uniqueness property is important to your application (i.e. you don't want multiple records with the same value for a field, e.g. email), you can define the UNIQUE constraint on a table. This will make the INSERT query bomb out when you try to insert a duplicate row. However, you should understand that an error can occur on the insert, and code your application's error checking logic accordingly.
Lookup the word DISTINCT.
Yes you can use the DISTINCT option.
select DISTINCT imageid,userid from Table A WHERE catid = XXXX