Proper use of mysql sum function - mysql

I have the following table:
Field | Type | Null | Key | Default | Extra |
+-----------------+----------------------+------+-----+---------+-------+
| SMILES | char(200) | NO | PRI | | |
| ConfRank | smallint(5) unsigned | NO | | NULL | |
| CompTime | double | YES | | NULL | |
I want to get the total CompTime for a specific SMILES (meaning Comptime are added for al confrank of a specific smiles).
I tried the following:
SELECT SMILES,(SUM(CompTime)) From GeoAndEnergies GROUP BY ConfRank WHERE SMILES='C';
And received this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'WHERE SMILES='C'' at line 1
I also tried:
SELECT SMILES,(SUM(CompTime)) From GeoAndEnergies GROUP BY ConfRank WHERE SMILES='C' GROUPBY ConfRank ;
And received another error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'WHERE SMILES='C' GROUPBY ConfRank' at line 1
What would be the correct syntax?

try the following
SELECT SMILES,(SUM(CompTime)) From GeoAndEnergies WHERE SMILES='C' GROUP BY ConfRank;

Your group by clause should appear after where clause.

so I used
SELECT SMILES, (SUM(CompTime)) From GeoAndEnergies WHERE Method ='PM6' GROUP BY SMILES INTO OUTFILE "/var/lib/mysql-files/data_PM6_timesum" ;
and
mysql> SELECT SMILES, (SUM(CompTime)) From GeoAndEnergies WHERE Method ='PM6' GROUP BY SMILES INTO OUTFILE "/var/lib/mysql-files/data_PM6_time" ;
and SELECT SMILES, NRotBond , NHeavyAtom From GeoAndEnergies WHERE Method ='PM6' AND ConfRank =1 INTO OUTFILE "/var/lib/mysql-files//data_PM6_rotheavy" ;
to create data_PM6_rotheavy and data_PM6_time.
By catting the 2 you should have what you want.

Your GROUP BY should follow the WHERE clause. Update as follows:
SELECT `SMILES`,
SUM(`CompTime`)
FROM `GeoAndEnergies`
WHERE `SMILES` = 'C'
GROUP BY `ConfRank`;

Related

How to get the last 3 rows from MYSQL database

I wanted to get only the last 3 rows of my database here is the query that I tried
select top 3 * from poem where MemberID = 54;
But there was an error
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '3 * from poem where MemberID = 54' at line 1
This is an example of one of the data stored in my table poem
MariaDB [bigproject]> select PoemID, Title, Description from poem where MemberID = 79;
+--------+------------------------+--------------------+
| PoemID | Title | Description |
+--------+------------------------+--------------------+
| 34 | Everything Has Changed | EHC |
| 52 | Kapono | Poetic |
| 53 | Love under the sky | How is your heart? |
| 54 | Imaginary | Imagine me |
+--------+------------------------+--------------------+
4 rows in set (0.000 sec)
I only wanted to get the last 3 rows of this.
Thank you!!

How can I LEFT JOIN a table and COUNT the rows?

I want to list my projects and the amount of posts each project has:
`projects` `posts`
------------------ ------------------
| `id` | `name` | | `id` | `name` |
------------------ ------------------
| 1 | Cat | | 1 | Cat |
| 2 | Dog | | 2 | Cat |
| 3 | Bird | | 3 | Dog |
| 4 | Frog | ------------------
------------------
$projects = $db->query('SELECT *,
COUNT(posts.*) AS posts
FROM projects
LEFT JOIN posts ON projects.name=posts.name
')->fetchAll(PDO::FETCH_ASSOC);
foreach($projects as $row) {
echo "Project ".$id." has ".$posts ".posts.".<br>";
}
So as a result I expect:
Project 1 has 2 posts.
Project 2 has 1 posts.
Project 3 has 0 posts.
Project 4 has 0 posts.
But I get an error message:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or
access violation: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near '*) AS posts,
So I changed posts.* into posts.id
But then my result is:
Project 1 has 3 posts.
I am actually now confused how to achieve the result I need.
The error was raised because of count(po.*) you must count for one column only inside of a count() function.
So, we want to find out how many times will find the 'name' column in posts table for a corresponding id column in projects table so we should count posts.name from posts joined with projects.
We need left join because we want also find out if it does not have corespondent (count(po.name) =0))
The query below will do the job:
SELECT p.*,COUNT(po.name) AS posts
FROM projects p
left join posts po on p.name = po.name
group by p.id
You can check here: http://sqlfiddle.com/#!9/3e9d4b/4

Why is MySQL matching strings and integers in a way I don't expect?

I have a mysql database with the following column:
+-----+
| vpn |
+-----+
| 11a |
When I use query:
SELECT vpn FROM vpn_map WHERE vpn=11;
It returns:
+-----+
| vpn |
+-----+
| 11a |
But if I query:
Select VPN from vpn_map where vpn=lla;
I get:
ERROR 1054 (42S22): Unknown column '11a' in 'where clause'
Why doesn't the previous query match? It will match if I do:
Select VPN from vpn_map where vpn='lla';
But then vpn='11' won't match anything. What am I missing here?
11a is not an integer and you have so use " around them otherwise you get an error.
SELECT vpn FROM vpn_map WHERE vpn="lla";

MySQL Cannot Show Column Name "Group"

I have a table which:
| fullname | onepreference | group | batch |
+------------+---------------+-------+-------+
| First Name | 1 | 1 | 2015 |
| First Name | 2 | 1 | 2015 |
| First Name | 1 | 2 | 2007 |
| First Name | 4 | 2 | 2014 |
+------------+---------------+-------+-------+
I am trying to write the following query:
SELECT
fullname,
onepreference,
group
FROM mytable
WHERE batch = 2015;
But it shows the following error
ERROR 1064 (42000): You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'group from mytable where batch=2015' at line 1
I think group column is a keyword of mysql. But I want to get the data of group column. Is there any possibilities available to take the group value?
group is a reserved word in mysql. so you have to escape it whith backticks:
select fullname, onepreference, `group` from add_application_form where batch=2015;
group is a reserved word and you need to use backticks:
SELECT
fullname,
onepreference,
`group`
FROM add_application_form
WHERE batch=2015;
Check the list here and avoid those keyword as table and column names.

Unexpected Operand should contain 1 columns error [duplicate]

This question already has answers here:
MySQL Syntax error message "Operand should contain 1 column(s)"
(5 answers)
Closed 8 years ago.
I have a table, exposure, which looks like this:
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| src | varchar(20) | NO | | NULL | |
| instrument | varchar(10) | NO | | NULL | |
| result | decimal(12,3) | NO | | NULL | |
| instant | bigint(20) | NO | | NULL | |
+------------+---------------+------+-----+---------+-------+
This table is used to store historical exposure values, and is expected to contain the occasional gap. I'm trying to select, for a given src and instant, all (instrument, result, instant)'s either at that target instant or at the largest instant smaller than the target instant. The following SELECT statement does exactly that.
SELECT * FROM exposure AS e
WHERE e.instant = (SELECT MAX(instant) FROM exposure
WHERE instant <= 1396985195077
AND src = 'testSrc')
AND e.src = 'testSrc';
However, when I try to specify the columns I wish to select as in the following query, MySQL responds with ERROR 1241 (21000): Operand should contain 1 column(s). I suspect it is confusing the inner and outer queries.
SELECT (e.instrument, e.result, e.instant) FROM exposure AS e
WHERE e.instant = (SELECT MAX(instant) FROM exposure
WHERE instant <= 1396985195077
AND src = 'testSrc')
AND e.src = 'testSrc';
My MySQL version string is: Server version: 5.5.34-0ubuntu0.13.04.1 (Ubuntu).
Change your SELECT (e.instrument, e.result, e.instant) to
SELECT e.instrument, e.result, e.instant
FROM ..
using columns in () mysql identifies it as any operation to perform thus there are multiple columns you get an error that there should be one column if you still wanna use parenthesis you can select your columns as
SELECT (e.instrument), (e.result), (e.instant)
FROM ..