MySQL count - attribute repeats itself more than once - mysql

My MySQL table (:students) structure is as follows:
id, version, count
My table looks like this:
id version count
1 James 8
2 Dan 3
3 Alice 2
4 James 1
5 James 3
6 Dan 5
I am trying to get the count of number times "James" has obtained a count equal or under 5.
My SQL query looks like this:
SELECT COUNT(*) AS version FROM students WHERE version='James' and count <=5
But I'm not getting any output. Am I missing something here?

you forgot to put the ; at the end of your query statement! Code should look as follows:
SELECT COUNT(*) AS version
FROM students
WHERE version='James' and count <=5;

Related

how to sum of a column with different values

I have a MySQL table which contains val_type column which have 3 type of values
id val_type company
1 rib 1
2 mod 2
3 rib 2
4 rib 3
5 mod 1
6 trop 1
$res= SELECT SUM(val_type) from tabl_name GROUP BY company;
with above query I get sum of all types in one
Result Required : Rib=3, mod=2 and trop=1
I want to get sum of all three types with one MySQL query. like how many rib,mod and trop.
Thanks
It sounds like you want to count all three types. You only need a basic GROUP BY query:
SELECT
val_type,
COUNT(*) AS cnt
FROM tabl_name
GROUP BY
val_type;

getting the right amount of beers for each email in database

I am a newbie in programming and just started with a project. My question is how should the query look like to get the right data?
My table:
id rfid beer email
1 12345 3 markus.test#gmail.com
2 54321 4 hans.test#gmail.com
3 63737 1 mark.test#gmail.com
4 12345 -2 markus.test#gmail.com
5 54321 -3 hans.test#gmail.com
The query result should look like
[{email: markus.test#gmail.com, beer:1},{email:hans.test#gmail.com, beer:1}....]
I know that you can use SUM(beer) in the query, but I don't know who to match them with the right email.
If you group your data then aggregate functions like sum() will sum for each group and not the complete result
select email, sum(beer) as beer
from your_table
group by email

mysql: specific item to be first and then to sort the rest chronologically to end then chronologically from beginning

Lets say I have the following table:
friends
_______
id name
1 johnny
2 tam
3 slick
4 mat
5 Rhanda
6 Tommy
7 ike
8 Spencer
9 Alan
I want to get all the friends list but I want the id 5 to be the first item in the list. I want the next item to follow chronologically until the end. Then the list start from the beginning until all results have been returned. So it should end like this...
friends
_______
id name
5 Rhanda
6 Tommy
7 ike
8 Spencer
9 Alan
1 johnny
2 tam
3 slick
4 mat
I have found and tried this but as you can imagine it's only returning the first item and then ordering the rest from 1 up. I have searched and worked on this for 2 days now and really could use some help. Any suggestions?
You can use:
order by (id >= 5) desc, id
This puts all ids 5 or greater first. Then it sorts each of the parts in ascending order.
MySQL treats boolean expressions as integers in an integer context, with 0 for false and 1 for true. So, to put the true values first, desc is needed.
You can add an expression to order clause like this:
select id, name from table
order by
case when id >= 5 then 0 else 1 end
, id

SQL query for incrementing values of row data through sequencing

I have row and column locations of several students.
Assuming number of rows and columns are fixed (to 3x3), how can I have a query result listing all row and column combinations, with students mapped to the correct location?
For example given these students data:
Student Row Column
Paul 1 1
Chris 1 3
James 2 2
Dwayne 3 3
How to have a query output like this:
Student Row Column
Paul 1 1
NULL 1 2
Chris 1 3
NULL 2 1
James 2 2
NULL 2 3
NULL 3 1
NULL 3 2
Dwayne 3 3
Please help! Thank you very much in advance.
While using PHP, Try mysql_insert_id() for your Code.
See Example here:
http://php.net/manual/en/function.mysql-insert-id.php
Good luck.
First of all, you need to know that Mysql haven't a implicit generator of N numbers of rows, like other RDBMS have, but you can emulate this using something like this:
http://use-the-index-luke.com/blog/2011-07-30/mysql-row-generator#mysql_generator_code
Take a look for study porpuse.
But for a first approach to resolve your problem, you can try this:
SELECT IFNULL((SELECT STUDENT FROM StudentSeatPlan B WHERE B.ROW = TB.ROW_ AND B.COLUMN = TB.COLUMN_),'') AS STUDENT,
TB.ROW_,TB.COLUMN_
FROM (
SELECT 1 ROW_,1 COLUMN_ UNION ALL
SELECT 1,2 UNION ALL
SELECT 1,3 UNION ALL
SELECT 2,1 UNION ALL
SELECT 2,2 UNION ALL
SELECT 2,3 UNION ALL
SELECT 3,1 UNION ALL
SELECT 3,2 UNION ALL
SELECT 3,3) TB
Whatever, it seems like you have a schema problem, something wrong it happens that you need generate data in this form in Mysql, maybe you prefered make it in your app if is the case.

MySQL where condition when intersecting 2 values (comma delimited)

I have table like this:
id products
------ ----------
5 1,2,3
6 2,4,5
9 1,4,7
17 4,6,7
18 1,6,8
19 2,3,6
I have to select only that rows, which row's products column contains one of (2,3) values.
In this case query must return:
id products
------ ----------
5 1,2,3
6 2,4,5
19 2,3,6
But I don't understand how to make construction of this query.
Any ideas?
Thanks in advance.
SELECT id,products
FROM yourTable
WHERE FIND_IN_SET('2',products)>0
OR FIND_IN_SET('3',products)>0
sqlFiddle
Would you mind to try this one please?
select * from TABLE_NAME where products regexp "(^|,)[23](,|$)";
Its doing either two or three at the begining, or at end. Or in between the commas.
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table. Then you can select normally.
Your table should look like this
id product
-- -------
5 1
5 2
5 3
6 2
6 4
6 5
...
With that table structure your select would be
select id
from your_normalized_table
where product in (2,3)
group by id
having count(distinct product) = 2
That query can make use of indexes and is really fast.