I am trying to create a trigger in MySQL database. I have a table (myData) with 3 columns. Date, Values, and Status. What I am trying to achieve is, when a new value comes, if it is higher than the last value, It should insert 1 in the Status column. If it is less than the last value, It should insert 0 in the status column. I couldn't find a logic to do it. Any suggestions, please?
BEGIN
IF new.Values > // what should be here?
THEN
INSERT INTO //
END
Instead consider the following:
SELECT * FROM my_table;
+----+-------+
| id | value |
+----+-------+
| 1 | 2 |
| 2 | 12 |
| 3 | 13 |
| 4 | 9 |
| 5 | 7 |
| 6 | 6 |
| 7 | 8 |
| 8 | 3 |
| 9 | 10 |
| 10 | 1 |
| 11 | 18 |
| 12 | 4 |
| 13 | 6 |
| 14 | 0 |
| 15 | 2 |
| 16 | 8 |
| 17 | 14 |
| 18 | 7 |
| 19 | 15 |
| 20 | 11 |
| 21 | 12 |
| 22 | 7 |
| 23 | 20 |
| 24 | 17 |
| 25 | 8 |
| 26 | 6 |
| 27 | 6 |
| 28 | 12 |
| 29 | 3 |
| 30 | 18 |
| 31 | 1 |
| 32 | 12 |
+----+-------+
SELECT a.*
, b.value >= a.value n
FROM my_table a
LEFT
JOIN
( SELECT x.*
, MIN(y.id) next
FROM my_table x
LEFT
JOIN my_table y
ON y.id > x.id
GROUP
BY x.id
) b
ON b.next = a.id;
+----+-------+------+
| id | value | n |
+----+-------+------+
| 1 | 2 | NULL |
| 2 | 12 | 0 |
| 3 | 13 | 0 |
| 4 | 9 | 1 |
| 5 | 7 | 1 |
| 6 | 6 | 1 |
| 7 | 8 | 0 |
| 8 | 3 | 1 |
| 9 | 10 | 0 |
| 10 | 1 | 1 |
| 11 | 18 | 0 |
| 12 | 4 | 1 |
| 13 | 6 | 0 |
| 14 | 0 | 1 |
| 15 | 2 | 0 |
| 16 | 8 | 0 |
| 17 | 14 | 0 |
| 18 | 7 | 1 |
| 19 | 15 | 0 |
| 20 | 11 | 1 |
| 21 | 12 | 0 |
| 22 | 7 | 1 |
| 23 | 20 | 0 |
| 24 | 17 | 1 |
| 25 | 8 | 1 |
| 26 | 6 | 1 |
| 27 | 6 | 1 |
| 28 | 12 | 0 |
| 29 | 3 | 1 |
| 30 | 18 | 0 |
| 31 | 1 | 1 |
| 32 | 12 | 0 |
+----+-------+------+
If all you want is the rows where n=1, then the query is actually even simpler...
SELECT a.*
FROM my_table a
JOIN
( SELECT x.*
, MIN(y.id) next
FROM my_table x
LEFT
JOIN my_table y
ON y.id > x.id
GROUP
BY x.id
) b
ON b.next = a.id
AND b.value >= a.value;
+----+-------+
| id | value |
+----+-------+
| 4 | 9 |
| 5 | 7 |
| 6 | 6 |
| 8 | 3 |
| 10 | 1 |
| 12 | 4 |
| 14 | 0 |
| 18 | 7 |
| 20 | 11 |
| 22 | 7 |
| 24 | 17 |
| 25 | 8 |
| 26 | 6 |
| 27 | 6 |
| 29 | 3 |
| 31 | 1 |
+----+-------+
Related
This question already has answers here:
MySQL - Selecting a Column not in Group By
(4 answers)
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
I am running below queries on my table.
Table:
+----+-------------+--------------+----------------------------+------------+--------+
| id | Qty_holding | Qty_reserved | created | tokenid_id | uid_id |
+----+-------------+--------------+----------------------------+------------+--------+
| 1 | 10 | 0 | 2018-01-18 10:52:14.957027 | 1 | 1 |
| 2 | 20 | 0 | 2018-01-18 11:20:08.205006 | 8 | 1 |
| 3 | 110 | 0 | 2018-01-18 11:20:21.496318 | 14 | 1 |
| 4 | 10 | 0 | 2018-01-23 14:26:49.124607 | 1 | 2 |
| 5 | 3 | 0 | 2018-01-23 15:00:26.876623 | 11 | 2 |
| 6 | 7 | 0 | 2018-01-23 15:08:41.887240 | 11 | 2 |
| 7 | 11 | 0 | 2018-01-23 15:22:48.424224 | 11 | 2 |
| 8 | 15 | 0 | 2018-01-23 15:24:03.419907 | 11 | 2 |
| 9 | 19 | 0 | 2018-01-23 15:24:26.531141 | 11 | 2 |
| 10 | 23 | 0 | 2018-01-23 15:27:11.549538 | 11 | 2 |
| 11 | 27 | 0 | 2018-01-23 15:27:24.162944 | 11 | 2 |
| 12 | 7.7909428 | 0.11459088 | 2018-01-23 15:27:24.168643 | 1 | 2 |
| 13 | 3 | 0 | 2018-01-23 15:36:51.412340 | 14 | 2 |
| 14 | 7.5585988 | 0.11459088 | 2018-01-23 15:36:51.417177 | 1 | 2 |
| 15 | 6 | 0 | 2018-01-24 08:43:46.635069 | 14 | 2 |
| 16 | 7.3262548 | 0.11459088 | 2018-01-24 08:43:46.639984 | 1 | 2 |
| 17 | 9 | 0 | 2018-01-24 10:09:08.207816 | 14 | 2 |
| 18 | 7.0939108 | 0.11459088 | 2018-01-24 10:09:08.212842 | 1 | 2 |
| 19 | 6 | 3 | 2018-01-24 13:43:08.929586 | 14 | 2 |
| 20 | 3 | 6 | 2018-01-24 14:49:56.960112 | 14 | 2 |
| 21 | 0 | 9 | 2018-01-24 14:50:33.423671 | 14 | 2 |
| 22 | 30 | 9 | 2018-01-24 14:51:14.865453 | 14 | 2 |
| 23 | 4.7704708 | 0.11459088 | 2018-01-24 14:51:14.870256 | 1 | 2 |
| 24 | 27 | 12 | 2018-01-24 14:56:56.914009 | 14 | 2 |
| 25 | 24 | 15 | 2018-01-24 14:57:56.475939 | 14 | 2 |
| 26 | 21 | 15 | 2018-01-24 14:58:06.750903 | 14 | 2 |
| 27 | 18 | 15 | 2018-01-24 15:02:43.203878 | 14 | 2 |
| 28 | 4.7705074 | 0.11459088 | 2018-01-24 15:02:43.224901 | 1 | 2 |
| 29 | 24 | 0 | 2018-01-24 15:03:40.421943 | 11 | 2 |
| 30 | 4.9535074 | 0.11459088 | 2018-01-24 15:03:40.441552 | 1 | 2 |
| 31 | 1 | 0 | 2018-01-26 10:35:33.173801 | 18 | 2 |
| 32 | 10 | 15 | 2018-01-26 12:46:03.780807 | 14 | 2 |
+----+-------------+--------------+----------------------------+------------+--------+
Query 1:
select uid_id
, tokenid_id
, max(created) as max_created
from accounts_userholding
group
by uid_id
, tokenid_id
+--------+------------+----------------------------+
| uid_id | tokenid_id | max_created |
+--------+------------+----------------------------+
| 1 | 1 | 2018-01-18 10:52:14.957027 |
| 1 | 8 | 2018-01-18 11:20:08.205006 |
| 1 | 14 | 2018-01-18 11:20:21.496318 |
| 2 | 1 | 2018-01-24 15:03:40.441552 |
| 2 | 11 | 2018-01-24 15:03:40.421943 |
| 2 | 14 | 2018-01-26 12:46:03.780807 |
| 2 | 18 | 2018-01-26 10:35:33.173801 |
+--------+------------+----------------------------+
Query 2:
select uid_id
, Qty_holding
, Qty_reserved tokenid_id
, max(created) as max_created
from accounts_userholding
group
by uid_id
, tokenid_id
+--------+-------------+--------------+------------+----------------------------+
| uid_id | Qty_holding | Qty_reserved | tokenid_id | max_created |
+--------+-------------+--------------+------------+----------------------------+
| 1 | 10 | 0 | 1 | 2018-01-18 10:52:14.957027 |
| 1 | 20 | 0 | 8 | 2018-01-18 11:20:08.205006 |
| 1 | 110 | 0 | 14 | 2018-01-18 11:20:21.496318 |
| 2 | 10 | 0 | 1 | 2018-01-24 15:03:40.441552 |
| 2 | 3 | 0 | 11 | 2018-01-24 15:03:40.421943 |
| 2 | 3 | 0 | 14 | 2018-01-26 12:46:03.780807 |
| 2 | 1 | 0 | 18 | 2018-01-26 10:35:33.173801 |
+--------+-------------+--------------+------------+----------------------------+
The Qty_holding value in above is not corresponding to latest date. For instance for tokenid_id 14 and uid_id as 2 latest record is
| 32 | 10 | 15 | 2018-01-26 12:46:03.780807 | 14 | 2 |
But above query is giving qty_holding as 3.
Any insights in functioning of mysql will be helpful . Thanks!
As a rule of thumb: When you mix normal columns with aggregate functions in SELECT, you need to use GROUP BY. Do not use GROUP BY when you do not have normal columns and aggregate functions in SELECT.
The thing to put into the GROUP BY, is all from SELECT but the aggregate functions (and possible constants).
As an example if you have a query:
select a, substring(b,3), 'x', max(y)
from yourtable
You need to use GROUP BY. You leave out 'x' as it is a constant and you leave out the aggregate function. The rest goes to the GROUP BY.
select a, substring(b,3), 'x', max(y)
from yourtable
group by a, substring(b,3)
Previous MySQL versions allowed quite liberal use of GROUP BY resulting quite often just bad/incorrect code.
Greetings I am trying to sum up expense value for each transaction.
Association table.
**Assoc table schema**
| PK_id | FK_transaction | FK_Expense |
|-------|----------------|------------|
| 1 | 1 | 85 |
| 2 | 2 | 81 |
| 3 | 3 | 77 |
| 4 | 4 | 83 |
| 5 | 5 | 84 |
| 6 | 6 | 105 |
| 7 | 7 | 104 |
| 8 | 8 | 71 |
| 9 | 8 | 88 |
| 10 | 8 | 90 |
Transaction table
**Transaction table schema**
| PK_id | type | value | confirmed_value |
|-------|------|-------|--------------------|
| 1 | 1 | 3.2 | 0 |
| 2 | 1 | 23.2 | 0 |
| 3 | 1 | 33.2 | 0 |
| 4 | 1 | 43.2 | 11.00 |
| 5 | 1 | 53.2 | 0 |
| 6 | 1 | 63.2 | 0 |
| 7 | 1 | 73.2 | 0 |
| 8 | 1 | 83.2 | 66.00 |
| 9 | 1 | 93.2 | 0 |
| 10 | 1 | 133.2 | 77.00 |
| 11 | 1 | 123.2 | 0 |
Expences Table
| PK_id | value |
|-------|-------|
| 85 | 3.2 |
| 81 | 23.2 |
| 77 | 33.2 |
| 83 | 43.2 |
| 84 | 53.2 |
| 105 | 63.2 |
| 104 | 73.2 |
| 71 | 83.2 |
| 88 | 93.2 |
| 90 | 133.2 |
Result ::
| PK_id | value | confirmed_value |
|-------|-------|-----------------|
| 1 | 3.2 | 0 |
| 2 | 23.2 | 0 |
| 3 | 33.2 | 0 |
| 4 | 43.2 | 11 |
| 5 | 53.2 | 0 |
| 6 | 63.2 | 0 |
| 7 | 73.2 | 0 |
| 8 | 83.2 | 66 |
| 8 | 93.2 | 66 |
| 8 | 133.2 | 66 |
Desired - before calculations ( just to give u the idea )
| PK_id | value | confirmed_value |
|-------|-------|-----------------|
| 1 | 3.2 | 0 |
| 2 | 23.2 | 0 |
| 3 | 33.2 | 0 |
| 4 | 43.2 | 11 |
| 5 | 53.2 | 0 |
| 6 | 63.2 | 0 |
| 7 | 73.2 | 0 |
| 8 | 309.6 | 66 |
Full desired result
count matching values COUNT(confirmed_value = value )
count entries that not match COUNT(confirmed_value != value )
/\ Calculate value of confirmed values
?? sum(transactions.confirmed_value)
and calculate value itself
Should be a row result ( example )
=======================================================================================================================
MATCHED | NOT_MATCHED | SUM(of values) | COUNT(of confirmed equal to value ) | COUNT(of confirmed ! equal to value )
=======================================================================================================================
1 | 10 | 3003.22 | 3 | 10
=======================================================================================================================
SQL Fiddle : http://sqlfiddle.com/#!9/2ab102/9
Thanks for any tips
Try this query -
SELECT transactions.PK_id,
SUM(expenses.value),
transactions.confirmed_value
FROM `assoc`
JOIN `expenses` ON `assoc`.`FK_Expense` = `expenses`.`PK_id`
JOIN `transactions` ON `assoc`.`FK_transaction` = `transactions`.`PK_id`
GROUP BY transactions.PK_id,
transactions.confirmed_value
Fiddle Demo - http://sqlfiddle.com/#!9/2ab102/14
how to generate all possible combinations from numbers in a MySql table with a certain length? For example 8 numbers.
Only with MySql not together with php.
My table "numbers" contains:
0
1
2
3
4
5
6
7
8
9
I want now to let create all possible combinations in "numbers1".
Like:
00000001
00000002
00000003
00000004
It sounds like you want a cartesian product but I don't get why you want a single column in your output but anyway
select distinct case when u1.id >= u.id then u.id else u1.id end umin,
case when u1.id < u.id then u.id else u1.id end umax
from users u cross join users u1
where u1.id <> u.id
order by umin,umax
The cross join creates the cartesian product the where clause drops those where the combination/permuatation results in the same value eg 1,1 2,2 etc and the distinct dedupes
So given this
MariaDB [sandbox]> select id from users;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 6 |
| 7 |
| 8 |
| 10 |
| 12 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
+----+
15 rows in set (0.00 sec)
The query results in
+------+------+
| umin | umax |
+------+------+
| 1 | 2 |
| 1 | 3 |
| 1 | 6 |
| 1 | 7 |
| 1 | 8 |
| 1 | 10 |
| 1 | 12 |
| 1 | 14 |
| 1 | 15 |
| 1 | 16 |
| 1 | 17 |
| 1 | 18 |
| 1 | 19 |
| 1 | 20 |
| 2 | 3 |
| 2 | 6 |
| 2 | 7 |
| 2 | 8 |
| 2 | 10 |
| 2 | 12 |
| 2 | 14 |
| 2 | 15 |
| 2 | 16 |
| 2 | 17 |
| 2 | 18 |
| 2 | 19 |
| 2 | 20 |
| 3 | 6 |
| 3 | 7 |
| 3 | 8 |
| 3 | 10 |
| 3 | 12 |
| 3 | 14 |
| 3 | 15 |
| 3 | 16 |
| 3 | 17 |
| 3 | 18 |
| 3 | 19 |
| 3 | 20 |
| 6 | 7 |
| 6 | 8 |
| 6 | 10 |
| 6 | 12 |
| 6 | 14 |
| 6 | 15 |
| 6 | 16 |
| 6 | 17 |
| 6 | 18 |
| 6 | 19 |
| 6 | 20 |
| 7 | 8 |
| 7 | 10 |
| 7 | 12 |
| 7 | 14 |
| 7 | 15 |
| 7 | 16 |
| 7 | 17 |
| 7 | 18 |
| 7 | 19 |
| 7 | 20 |
| 8 | 10 |
| 8 | 12 |
| 8 | 14 |
| 8 | 15 |
| 8 | 16 |
| 8 | 17 |
| 8 | 18 |
| 8 | 19 |
| 8 | 20 |
| 10 | 12 |
| 10 | 14 |
| 10 | 15 |
| 10 | 16 |
| 10 | 17 |
| 10 | 18 |
| 10 | 19 |
| 10 | 20 |
| 12 | 14 |
| 12 | 15 |
| 12 | 16 |
| 12 | 17 |
| 12 | 18 |
| 12 | 19 |
| 12 | 20 |
| 14 | 15 |
| 14 | 16 |
| 14 | 17 |
| 14 | 18 |
| 14 | 19 |
| 14 | 20 |
| 15 | 16 |
| 15 | 17 |
| 15 | 18 |
| 15 | 19 |
| 15 | 20 |
| 16 | 17 |
| 16 | 18 |
| 16 | 19 |
| 16 | 20 |
| 17 | 18 |
| 17 | 19 |
| 17 | 20 |
| 18 | 19 |
| 18 | 20 |
| 19 | 20 |
+------+------+
105 rows in set (0.00 sec)
if the length is fixed it is quite simple, so you could do something like this
DECLARE #number TABLE (num NVARCHAR(1))
INSERT INTO #number
VALUES ('0'),('1'),('2'),('3'),('4'),('5'),('6'),('7'),('8'),('9')
SELECT n1.num + n2.num + n3.num + n4.num
FROM #number n1, #number n2, #number n3, #number n4
if the number is variable you could either build the sql-command and execute it using sp_execute #cmd or you can create a table-value-function for it.
Edit: I just overread the "mysql"-part. the solution above is coded in TSQL (using MS SqlServer). I am not completely sure, but the only thing you have to change should be the declaration of the variable.
CREATE TEMPORARY TABLE #number (num NVARCHAR(1))
And if I am not mistaken instead of using sp_executeto execute a command you can use something like
PREPARE cmd FROM 'SELECT ...'
EXECUTE cmd
I have a table test and the structure is as shown below
mysql> select * from test;
+----+------------+------------+---------+-----+------+------+
| id | is_deleted | sort_order | version | cid | pid | qid |
+----+------------+------------+---------+-----+------+------+
| 14 | | 6 | 0 | 1 | 2 | 7 |
| 18 | | 1 | 2 | 1 | 4 | 12 |
| 26 | | 9 | 0 | 1 | 1 | 1 |
| 40 | | 1 | 0 | 1 | 5 | 18 |
| 49 | | 2 | 0 | 1 | 5 | 35 |
| 50 | | 3 | 0 | 1 | 5 | 39 |
| 51 | | 4 | 0 | 1 | 5 | 40 |
| 61 | | 10 | 0 | 1 | 1 | 2 |
| 62 | | 11 | 0 | 1 | 1 | 3 |
| 63 | | 12 | 0 | 1 | 1 | 4 |
| 64 | | 13 | 0 | 1 | 1 | 42 |
| 65 | | 14 | 0 | 1 | 1 | 43 |
| 66 | | 2 | 0 | 1 | 4 | 26 |
| 67 | | 3 | 0 | 1 | 4 | 38 |
| 71 | | 7 | 0 | 1 | 2 | 2 |
| 72 | | 8 | 0 | 1 | 2 | 8 |
| 73 | | 9 | 0 | 1 | 2 | 50 |
| 74 | | 10 | 0 | 1 | 2 | 51 |
| 76 | | 3 | 0 | 1 | 3 | 9 |
| 78 | | 4 | 0 | 1 | 3 | 53 |
| 79 | | 1 | 0 | 1 | 6 | 54 |
| 80 | | 1 | 0 | 1 | 11 | 59 |
| 81 | | 1 | 0 | 1 | 12 | 70 |
+----+------------+------------+---------+-----+------+------+
I want to set version to 1 of all the rows whose cid =1,pid not equal to 6,11 and qid not equal to 28,59,54
Can anybody please tell me how to do?
UPDATE test
SET `version` = 1
WHERE cid =1
AND pid NOT in(6,11)
AND qid NOT IN (28,59,54);
This question already has answers here:
Closed 10 years ago.
There are two tables
Table 1
select * from manage_tcp;
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 2 | 1433 | up |
| 2 | 3389 | up |
| 3 | 3389 | down |
| 3 | 1433 | down |
| 9 | 3389 | up |
| 8 | 3389 | up |
| 7 | 1433 | up |
| 6 | 3389 | up |
| 5 | 3389 | up |
| 4 | 3389 | up |
| 10 | 1433 | up |
| 11 | 1433 | up |
| 12 | 3389 | up |
| 13 | 1433 | up |
| 14 | 3389 | up |
| 15 | 1433 | up |
| 16 | 3389 | up |
| 17 | 1433 | up |
| 18 | 3389 | up |
| 19 | 1433 | up |
| 20 | 3389 | up |
| 21 | 1433 | up |
| 24 | 1433 | up |
| 23 | 3389 | up |
| 25 | 3389 | up |
| 26 | 1433 | up |
| 29 | 1433 | up |
| 28 | 3389 | up |
| 30 | 3389 | up |
| 31 | 80 | up |
| 32 | 3389 | up |
| 33 | 80 | up |
| 34 | 3389 | up |
| 35 | 80 | up |
+----+----------+--------+
34 rows in set (0.00 sec)
Table 2
mysql> select * from manage_host;
+----+------------+-------------+--------+-------+--------------+----------+------+
| id | uptime | type | statut | group | thresold_ref | thresold | mail |
+----+------------+-------------+--------+-------+--------------+----------+------+
| 2 | 277248529 | win2003.png | up | 4 | 1 | 0 | NULL |
| 3 | 277277471 | win2003.png | prob | 4 | 1 | 0 | NULL |
| 4 | 346159833 | win2003.png | up | 4 | 1 | 0 | NULL |
| 5 | 930205491 | win2003.png | up | 5 | 1 | 0 | NULL |
| 6 | 3805663007 | win2003.png | up | 5 | 1 | 0 | NULL |
| 7 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 8 | 29413867 | win2003.png | up | 5 | 1 | 0 | NULL |
| 9 | 981986401 | win2003.png | up | 5 | 1 | 0 | NULL |
| 10 | -1 | win2003.png | up | 5 | 1 | 0 | NULL |
| 11 | -1 | win2003.png | up | 5 | 1 | 0 | NULL |
| 12 | 2230787611 | win2003.png | up | 7 | 1 | 0 | NULL |
| 13 | -1 | win2003.png | up | 7 | 1 | 0 | NULL |
| 14 | 1004161923 | win2003.png | up | 8 | 1 | 0 | NULL |
| 15 | -1 | win2003.png | up | 8 | 1 | 0 | NULL |
| 16 | 1592294954 | win2003.png | up | 8 | 1 | 0 | NULL |
| 17 | -1 | win2003.png | up | 8 | 1 | 0 | NULL |
| 18 | 1449216250 | win2003.png | up | 4 | 1 | 0 | NULL |
| 19 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 20 | 3461945234 | win2003.png | up | 4 | 1 | 0 | NULL |
| 21 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 23 | 3461946968 | win2003.png | up | 4 | 1 | 0 | NULL |
| 24 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 25 | 3462022562 | win2003.png | up | 4 | 1 | 0 | NULL |
| 26 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 35 | 3318333401 | win2003.png | up | 6 | 1 | 0 | |
| 28 | 3461879984 | win2003.png | up | 4 | 1 | 0 | NULL |
| 29 | -1 | win2003.png | up | 4 | 1 | 0 | NULL |
| 30 | 1872950109 | win2003.png | up | 6 | 1 | 0 | NULL |
| 31 | 1872950125 | win2003.png | up | 6 | 1 | 0 | NULL |
| 32 | 1955232033 | win2003.png | up | 6 | 1 | 0 | NULL |
| 34 | 3318333401 | win2003.png | up | 6 | 1 | 0 | |
| 33 | 1955232049 | win2003.png | up | 6 | 1 | 0 | NULL |
+----+------------+-------------+--------+-------+--------------+----------+------+
32 rows in set (0.00 sec)
I'd like to compare those two tables above and to get the results in table 1 but not in table 2 like: (this is the result i want)
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 2 | 1433 | up |
| 3 | 3389 | down |
| 3 | 1433 | down |
+----+----------+--------+
I've tried
SELECT a.*,b.*
FROM manage_tcp as a
LEFT JOIN manage_host as b
USING(id,statut)
WHERE b.id is null;
but I got
+----+----------+--------+
| id | services | statut |
+----+----------+--------+
| 3 | 3389 | down |
| 3 | 1433 | down |
+----+----------+--------+
it's not what I want.
Thanks a lot!
Try this:
SELECT m.id, m.services, m.statut
FROM manage_tcp m
WHERE EXISTS
(
SELECT *
FROM manage_host mh
WHERE mh.id = m.id AND m.statut = mh.statut
)
This will give you the intersection of the tables manage_tcp and manage_host based on id and status columns.
get the results in table 1 but not in table 2
If you need the difference between the tow tables, use NOT EXISTS instead of EXISTS like this:
SELECT m.id, m.services, m.statut
FROM manage_tcp m
WHERE NOT EXISTS
(
SELECT *
FROM manage_host mh
WHERE mh.id = m.id AND m.statut = mh.statut
)
Typically, this is the implementations of the INTERSECT and EXCEPT operators which are defined by the SQL standard as the set intersection and difference operators. But, unfortunately, most database system doesn't implement these operators. MS SQL Server does support these operators but not MYSQL .
Try this: (assume the comparison column are id and statut)
SELECT a.id, a.services, a.statut
FROM manage_tcp a
WHERE NOT EXISTS(SELECT b.id FROM manage_host b WHERE b.id=a.id AND b.statut=a.statut)
UNION
SELECT a.id, (SELECT services FROM manage_tcp WHERE id=a.id ORDER BY id LIMIT 1), a.statut
FROM manage_tcp a
GROUP BY a.id, a.statut HAVING COUNT(1)>1
The first query will produce result from table a where not in table b.
The second, after UNION, will produce result for id that appear twice in table a.
SELECT manage_tcp.* FROM
(
SELECT a.id,a.statut,a.cnt
FROM (
SELECT id, statut, COUNT(1) AS cnt FROM manage_tcp GROUP BY id,statut
) AS a
LEFT JOIN manage_host AS b USING(id, statut)
GROUP BY a.id, a.statut
HAVING a.cnt > COUNT(1)
) AS diff
LEFT JOIN manage_tcp USING(id,statut)
or
SELECT a.id,a.statut,a.services,a.cnt
FROM (
SELECT id,statut,services,COUNT(1) AS cnt FROM manage_tcp GROUP BY id,statut
) AS a
LEFT JOIN manage_host AS b USING(id, statut)
GROUP BY a.id,a.statut
HAVING a.cnt > COUNT(1)