MySQL - how to get count of rows - mysql

I have these data in database:
+----+------------+---------+
| id | col1 | col2 |
+----+------------+---------+
| 1 | 3 | 2 |
| 2 | 2 | 3 |
| 3 | 4 | 5 |
+----+------------+---------+
I am trying to do a query, that will give me a count of rows, in which are the same numbers.
I know the value one of them (the numbers).
In this case, the same numbers are 2 and 3 (in the columns col1 and col2) - I am trying to get from database number 2 (two rows). I have available always one of two numbers - in this case number 3.
Is possible to do this query?
Thanks for help.

For the very narrow scenario you specified, I would try this:
select count(*)
from myTable mt1
inner join myTable mt2 on (mt1.col1 = mt2.col2 AND mt2.col1 = mt1.col2)
and mt1.col1 = 3 --here goes the known value
This only works for the data you posted. Please let us know the count for the following scenario:
+----+------------+---------+
| id | col1 | col2 |
+----+------------+---------+
| 1 | 3 | 2 |
| 2 | 2 | 3 |
| 3 | 3 | 2 |
| 4 | 3 | 5 |
| 5 | 4 | 5 |
+----+------------+---------+

Are you looking something like
SELECT (col1+col2) , COUNT(*) AS num_rows
FROM table1
WHERE col1 =3 OR col2 =3
GROUP BY (col1+col2)
HAVING num_rows >1

With this query:
select count(*) count_rows
from table1
where col1=3 or col2=3
group by concat(IF(col1<col2, col1, col2), ',', IF(col1<col2, col2, col1))
you get the result 2.
Replace the 3 from the query in the where clause if there is another input.
The value for concat(IF(col1<col2, col1, col2), ',', IF(col1<col2, col2, col1)) would be "2,3" for records 1-2, (and "4,5" for the third record).

Related

How to limit a query in discrete chunks? MySQL

Is there a way to limit a query in discrete groups? For example, let's say I have this query below.
| col1 | col2 |
---------------
| 1 | A |
| 1 | B |
| 2 | C |
| 2 | D |
| 3 | E |
| 3 | F |
I want the limit on this query to be 5 rows. However, I only want it to show discrete complete groups based on the first column. So that means I don't want to show (3, E) since (3, F) would be cut off. So it would only show the first 4 rows.
Is there a way to write this dynamic logic into a MySQL query?
Count rows in a subquery:
select col1, col2
from mytable m1
where (select count(*) from mytable m2 where m2.col1 <= m1.col1) <= 5
order by col1, col2;

Implement a union all query with an inner join

I have 2 table like this:
// table1
+-----------------+-----------------+
| col1 | id |
|-----------------+-----------------|
+-----------------+-----------------+
| test | 1 |
|-----------------+-----------------|
| test | 2 |
|-----------------+-----------------|
| anything | 3 |
|-----------------+-----------------|
| anything | 4 |
|-----------------+-----------------|
// table2
+-----------------+-----------------+
| col1 | id |
|-----------------+-----------------|
+-----------------+-----------------+
| test | 5 |
|-----------------+-----------------|
| test | 6 |
|-----------------+-----------------|
| anything | 7 |
|-----------------+-----------------|
| anything | 8 |
|-----------------+-----------------|
When I use union all to get id values where col1 is equal to 'test', result is desired:
select * from table1 where col1='test'
union all
select * from table2 where col1='test'
// the result of this code is: 4 rows. id{1,2,5,6}
Then, for faster and better performance I implemented it with an inner join, but result is not desired:
select * from table1 t1 inner join table2 t2
on t1.col1=t2.col1
where t1.col1='test'
// the result of this code is: 8 rows. id{1-5,1-6,2-5,2-6}
How can I use an inner join with these tables to get result id{1, 2, 5, 6}?
EDIT
example:
table1 {[col1]=word, [col2]=mean}
+-----+------------------------------------------------------------------------------------------+
| a | used when referring to someone or something for the first time in a text or conversation |
|-----|------------------------------------------------------------------------------------------|
| a | used to indicate membership of a class of people or things |
|-----|------------------------------------------------------------------------------------------|
| x | xxxxx |
+-----+------------------------------------------------------------------------------------------+
table2 {[col1]=word, [col2]=mean}
+-----+------------------------------------------------------------------------------------------+
| a | the blood group whose red cells carry the A antigen |
|-----|------------------------------------------------------------------------------------------|
| x | xxxxx |
+-----+------------------------------------------------------------------------------------------+
Now is it possible to I use join and echo this? :
a | used when referring to someone or something for the first time in a text or conversation
a | used to indicate membership of a class of people or things
a | the blood group whose red cells carry the A antigen
You cannot do this easily with an inner join. Think about what an inner join does, it places them adjacent to each other based on the related column. For example, if you ran the following query:
SELECT *
FROM table1
JOIN table2 ON table2.col1 = table1.col1 AND table2.col1 = 'test';
You would see a result like this:
| col1 | id | col1 | id |
+------+----+------+----+
| test | 1 | test | 5 |
| test | 2 | test | 5 |
| test | 1 | test | 6 |
| test | 2 | test | 6 |
At this point, you might try to run a query for distinct values from each of the two columns but to the best of my knowledge that is not possible.
So, I don't believe you can replace a UNION ALL query with an INNER JOIN, or any join for that matter. Even if you preformed a cross join, you will only ever get table1.id in its own column, and table2.id in a separate column, which results in the same problem as above.
EDIT
When you use union all, you're just combining rows from the table. So if I run the following query:
SELECT col1, id
FROM table1
WHERE col1 = 'test'
UNION ALL
SELECT col1, id
FROM table2
WHERE col1 = 'test'
You will see this:
| col1 | id |
+------+----+
| test | 1 |
| test | 2 |
| test | 5 |
| test | 6 |
Because it takes the result set from the two individual queries and combines them together. Here is an SQL Fiddle example that shows both queries so you can visually see the difference side by side.

I need to get the average for every 3 records in one table and update column in separate table

Table Mytable1
Id | Actual
1 ! 10020
2 | 12203
3 | 12312
4 | 12453
5 | 13211
6 | 12838
7 | 10l29
Using the following syntax:
SELECT AVG(Actual), CEIL((#rank:=#rank+1)/3) AS rank FROM mytable1 Group BY rank;
Produces the following type of result:
| AVG(Actual) | rank |
+-------------+------+
| 12835.5455 | 1 |
| 12523.1818 | 2 |
| 12343.3636 | 3 |
I would like to take AVG(Actual) column and UPDATE a second existing table Mytable2
Id | Predict |
1 | 11133
2 | 12312
3 | 13221
I would like to get the following where the Actual value matches the ID as RANK
Id | Predict | Actual
1 | 11133 | 12835.5455
2 | 12312 | 12523.1818
3 | 13221 | 12343.3636
IMPORTANT REQUIREMENT
I need to set an offset much like the following syntax:
SELECT #rank := #rank + 1 AS Id , Mytable2.Actual FROM Mytable LIMIT 3 OFFSET 4);
PLEASE NOTE THE AVERAGE NUMBER ARE MADE UP IN EXAMPLES
you can join your existing query in the UPDATE statement
UPDATE Table2 T2
JOIN (
SELECT AVG(Actual) as AverageValue,
CEIL((#rank:=#rank+1)/3) AS rank
FROM Table1, (select #rank:=0) t
Group BY rank )T1
on T2.id = T1.rank
SET Actual = T1.AverageValue

MySQL select from specific ID until match condition

Given this table:
+----+-----------+--------+
| id | condition | values |
+----+-----------+--------+
| 1 | a | 1 |
+----+-----------+--------+
| 2 | a | 2 |
+----+-----------+--------+
| 3 | a | 3 |
+----+-----------+--------+
| 4 | a | 4 |
+----+-----------+--------+
| 5 | b | 5 |
+----+-----------+--------+
| 6 | b | 6 |
+----+-----------+--------+
How can I get a new table that begins on id=3 (including) and goes until condition = b (excluding):
+----+-----------+--------+
| id | condition | values |
+----+-----------+--------+
| 3 | a | 3 |
+----+-----------+--------+
| 4 | a | 4 |
+----+-----------+--------+
added fiddle: http://sqlfiddle.com/#!2/9882f7
Basically I want a table between a matching first condition (over a specific column - id) and a second one (over a different column - condition)
You need to stop thinking of SQL data as having any order. Think of SQL data in sets; you have to search by values, not by positions.
SELECT t1.*
FROM t AS t1
JOIN (
SELECT MIN(id) AS id FROM t
WHERE id >= 3 AND `condition` = 'b'
) AS t2
WHERE t1.id >= 3 AND t1.id < t2.id
ORDER BY t1.id
Something like this:
select t.*
from table t
where id >= 3 and id < (select min(t2.id) from table t2 where t2.condition = 'b');
EDIT:
This query works fine on the SQL Fiddle:
select t.*
from t
where id >= 3 and id < (select min(t2.id) from t t2 where t2.condition = 'b');
If I understand what you are asking for, I believe this will work for you:
SELECT id, condition, values
FROM tableName
WHERE id > 2
AND condition != b
ORDER BY id
I hope that works for you.

mysql select ordernumber by group

I'm trying to do something like 'select groupwise maximum', but I'm looking for groupwise order number.
so with a table like this
briefs
----------
id_brief | id_case | date
1 | 1 | 06/07/2010
2 | 1 | 04/07/2010
3 | 1 | 03/07/2010
4 | 2 | 18/05/2010
5 | 2 | 17/05/2010
6 | 2 | 19/05/2010
I want a result like this
breifs result
----------
id_brief | id_case | dateOrder
1 | 1 | 3
2 | 1 | 2
3 | 1 | 1
4 | 2 | 2
5 | 2 | 1
6 | 2 | 3
I think I want to do something like described here MySQL - Get row number on select, but I don't know how I would reset the variable for each id_case.
This will give you how many records are there with this id_case value and a date less than or equal to this date value.
SELECT t1.id_brief,
t1.id_case,
COUNT(t2.*) AS dateOrder
FROM yourtable AS t1
LEFT JOIN yourtable AS t2 ON t2.id_case = t1.id_case AND t2.date <= t1.date
GROUP BY t1.id_brief
Mysql is permissive about columns which can be queries using GROUP BY. With a more stric DBMS you may need GROUP BY t1.id_brief, t1.id_case.
I strongly advise you to have the right indexes on the table:
CREATE INDEX filter1 ON yourtabl (id_case, date)