Mysql statement, if at least one row contains result - mysql

i've a table with records:
----------------------------------
ID | UniqueId | Name | Result
----------------------------------
1 1 Test1 OK
2 1 Test1 Cancelled
3 1 Test1 OK
4 2 Test2 OK
5 2 Test2 OK
6 2 Test2 OK
7 2 Test2 OK
8 3 Test3 OK
9 3 Test3 OK
Let's say i wan't to check if at least one row with UniqueId = 1 not contains Result == Cancelled. To exclude record with UniqueId = 1, because it is cancelled.
How can i do this?
Thank you

SELECT t1.* from table as t1 where t1.UniqueId not in(select t.UniqueId from table as t
where t.Result="Cancelled")

Just ask for rows with UniqueId = 1 and Result != Cancelled
SELECT ID FROM table WHERE UniqueId = 1 AND Result <> 'Cancelled' LIMIT 1

I'd go this way, but the other answers are a bit easier in terms of syntax.
SELECT UniqueId, Name
FROM Table T
GROUP BY UniqueId, Name
HAVING Result != 'Cancelled'

SELECT id FROM mytable WHERE uniqueId = 1 AND result != 'Cancelled'

Related

MySQL count two rows as one

I need to count matches in a database.
Input:
id_to id_from
1 2
2 1
1 3
3 1
1 4
5 1
the 5th and 6th row has only one direction so doesn't count
Sample Output:
id_match
1
2
3
So, for 1 (implicit), 2 and 3 there is a reverse match but for 4 and 5 there aren't.
---- EDITED ----
Supposing the table name is "example" and I want to get all matches of id=1 then the SQL query will be:
SELECT count(*) FROM
(SELECT id_to FROM example WHERE id_from = 1) as t1,
(SELECT id_from FROM example WHERE id_to = 1) as t2
WHERE t1.id_to = t2.id_from
but maybe there is a better way to do it
You could try
SELECT DISTINCT id_from AS matched_id
FROM your_table AS data1
WHERE EXISTS (SELECT 1
FROM your_table AS data2
WHERE data1.id_from = data2.id_to
AND data1.id_to = data2.id_from)
I've created a demo here

UPDATE all, except last

I have a table like this:
table_documents
document_id
document_folder_id
document_title
document_notify_expired
ID FOLDER TITLE Notify Expired
1 2 Test1 1
2 2 Test2 1
3 2 Test3 1
4 2 Test4 1
5 2 Test5 1
I'm like to UPDATE and set document_notify_expired to 0 for all records EXCEPT last, for a specific folder like below
ID FOLDER TITLE Notify Expired
1 2 Test1 0
2 2 Test2 0
3 2 Test3 0
4 2 Test4 0
5 2 Test5 1
Here my code but not update as expected
UPDATE table_documents docs
LEFT OUTER JOIN ( SELECT * FROM table_documents ORDER BY document_id DESC LIMIT 1 )last_doc ON last_doc.document_id = docs.document_id
SET doc.document_notify_expired = '0'
WHERE document_folder_id = '2'
AND last_doc.document_notify_expired = '1'
Try this out
UPDATE table_documents docs
INNER JOIN
(SELECT
MAX(id) id
FROM
table_documents) docsmax ON docs.id != docsmax.id
SET
document_notify_expired = 0;
Obviously the last row has the greatest id, so this row is not going to be there after the join, which will returns all the other rows and you can play with them as you wish.
update table1.table_documents
set table1.document_notify_expired = 0
where table1.document_folder_id = 2
and not table1.document_id = (
select table2.document_id
from table_documents as table2
where table2.document_folder_id = 2
order by table2.document_id desc
limit 1
);

MYSQL query data and combine, group and order results

I am having trouble getting a query to work effectively which only queries 1 table. Here is an example of the table data:
ID NAME PARENT_ID SORT_ORDER
1 Home NULL 1
2 Contact NULL 3
3 Service NULL 2
4 Service1 3 0
5 Service3 3 2
6 Service2 3 1
What I would like to do is to return the data from this table with results that have a PARENT_ID appearing under the result with that ID, and to have all results then display in their SORT_ORDER. Here is how I would like the query to result the above data:
ID NAME PARENT_ID SORT_ORDER
1 Home NULL 1
3 Service NULL 2
4 Service1 3 0
6 Service2 3 1
5 Service3 3 2
2 Contact Null 3
Any feedback to make this happen is very welcome.
Kind regards,
Paul
If I understand your logic correctly, you could use this:
SELECT
t1.*
FROM
yourtable t1 LEFT JOIN yourtable t2
ON t1.PARENT_ID = t2.ID
ORDER BY
COALESCE(t2.SORT_ORDER, t1.SORT_ORDER),
t1.PARENT_ID IS NOT NULL,
SORT_ORDER
Please see fiddle here.

select only first and then ignore rows where values in two colums are the same

Example table:
id foo bar
1 2 877
2 2 877
3 3 678
4 4 887
5 1 678
6 2 887
Example results:
id foo bar
3 3 678
4 4 887
5 1 678
6 2 887
The sql ignores the duplicates of 2 and 887 but not the latest one.
Foo will be specified beforehand.
How can this be achieved in MYSQL?
If I got you right you always want the bar value to a foo with the highest id.
select *
from your_table t1
where id = (select max(id) from your_table t2
where t2.foo = t1.foo)
Here is an SQL Fiddle for a demo. It also gives an alternative way with a left join. The construct does assume that the "latest one" is the one with the highest id.
EDIT: So according to your comment you only want that "highest id" for foo=2, that is a simply OR logic. I don't know if foo can be null so I added the IS NULL condition otherwise the <> might not work.
select * from your_table t1
where foo <> 2 or foo is null
or (foo = 2 and id = (select max(id) from your_table t2
where t2.foo = t1.foo) ) ;
Here is a demo

Selecting rows with reference id's to same table from mysql

I have a table such as:
id name ref_id order data_obj
-- ---- ------ ----- --------
1 Sam 0 15 [binary data]
2 Jack 0 20 [binary data]
3 Sue 0 25 [binary data]
4 Sam2 1 - [no data]
5 Sue2 3 - [no data]
6 Sam3 1 - [no data]
The idea is that I have more columns other than data_obj which can be common, so I don't want to insert them again, just want to insert a reference id to the same data.
Is it possible to write a query and select this:
1 - Sam - binary data from id 1
4 - Sam2 - binary data from id 1
6 - Sam3 - binary data from id 1
2 - Jack - binary data from id 2
3 - Sue - binary data from id 3
5 - Sue2 - binary data from id 3
Please note that I'm ordering according to column named order and there's no actual data for this column for referenced rows.
SELECT t1.id, t1.name, t2.data_obj
FROM your_table t1
LEFT JOIN your_table t2 ON t1.ref_id = t2.id
ORDER BY t1.order
Other version, which doesn't return rows without ref
SELECT t1.id, t1.name, t2.data_obj
FROM your_table t1, your_table t2
WHERE t1.ref_id = t2.id
ORDER BY t1.order
Here's a modification of #vartec's answer. This modification uses COALESCE() to combine the data_obj from either the primary row or the referenced row.
SELECT t1.id, t1.name, COALESCE(t1.data_obj, t2.data_obj)
FROM your_table t1
LEFT JOIN your_table t2 ON t1.ref_id = t2.id
ORDER BY COALESCE(t1.order, t2.order), ref_id;
COALESCE() is a standard SQL function that returns its first non-NULL argument.
Why aren't you using more than one table?
CREATE TABLE user (
user_id number not null (some form of auto increment or sequence),
name varchar(50) not null,
otherdata type,
primary key (id));
CREATE TABLE common (
common_id number not null (autoinc),
user_id number not null,
commondata type,
primary key (common_id),
unique index (user_id, common_id));
SELECT u.name, u.otherdata, c.commondata
FROM user u, common c
WHERE u.user_id = c.user_id
TABLE user
user_id name otherdata
1 Sam abc
2 Jack def
3 Sue ghi
Table common
common_id user_id commondata
1 1 AAA
2 1 BBB
3 1 CCC
4 2 DDD
5 3 EEE
6 3 FFF
Output
name otherdata commondata
Sam abc AAA
Sam abc BBB
Sam abc CCC
Jack def DDD
Sue ghi EEE
Sue ghi FFF