SELECT * FROM table WHERE id != 4;
SELECT * FROM table WHERE NOT id = 4;
SELECT * FROM table WHERE id <> 4;
I've got this all working but I also have to choose another field (or more fields) to decide what rows are returned.
How can I get this working?
If you want to 'deselect' columns where both conditions are true (ID1 is 4 and ID2 is 7), use something like:
select * from TBL where ID1 <> 4 or ID2 <> 7;
ID1 ID2 selected
--- --- --------
4 7 no
4 1 yes
1 7 yes
1 1 yes
If you want to 'deselect' columns where either condition is true (ID1 is 4 or ID2 is 7), use something like:
select * from TBL where ID1 <> 4 and ID2 <> 7;
ID1 ID2 selected
--- --- --------
4 7 no
4 1 no
1 7 no
1 1 yes
This can be extended to more conditions simply by adding them to the end of the where clause (and changing both/either to all/any in the text).
select * from albums where idAlbum!= 4 and idAlbum!= 8 I just solve my problem. Thanks for the help guys!
Related
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'
I feel very very stupid now because I have a problem and cannot seem to figure it out.
Very simple MySQL table with 2 columns :
ID1 | ID2
1 | 1
1 | 2
2 | 1
Don't know very good how to explain the conditions : I want to select the value 1 from the column ID1 because it has connections with the values 1 AND 2 from ID2.
It's somewhat the opposite of IN.
If I make
SELECT ID1 FROM X WHERE ID2 IN (1,2) I recieve both 1 and 2 because it is a reunion. I want an intersection, something like SELECT ID1 FROM X WHERE ID2 IN BOTH 1 AND 2.
I am fairly sure it has something to do with grouping.
1 solution is to make
SELECT * FROM
(SELECT ID1, GROUP_COCAT(ID2) y
FROM X
GROUP BY ID1)t
WHERE t.y = '1,2'
but this is NOT ok because I do not know the order ( 1,2 or 2,1 ) and I can have more values.
Hopefully this is clear enough, I am very tired.
SELECT t.*
FROM TEMP t
WHERE t.id2 IN (1, 2)
GROUP BY t.id1 HAVING COUNT(*) = 2
OR
SELECT t.*
FROM TEMP t
WHERE t.id2 IN (1, 2, 3, 4)
GROUP BY t.id1 HAVING COUNT(*) = 4
I have a table with only an id field, I would get the result of this field, as distintic id and another column all have different IDs and can not be equal and also results already found previously...EX:
Id_field
1
2
3
I want the following result:
1 - 2
1 - 3
2 - 3
I d'nt
1 - 1
2 - 2
3 - 3
and result previously stated
2 - 1
3 - 1
3 - 2
Simple self join?
SELECT a.id_field, b.id_field
FROM SomeTable a
INNER JOIN SomeTable b
ON a.id_field < b.id_field
SELECT t2.id AS id2,t1.id AS id1
FROM t AS t1
JOIN t AS t2 ON (t1.id > t2.id);
SQLFIDDLE
I have a table like so;
id1 id2
1 1
2 1
1 2
3 2
1 3
4 3
1 4
5 4
I'd like to select it in a way that I'd get rows GROUPed by id2, but still preserving both values of id1 for the corresponding rows in the table.
So I'd get a result set like so;
id1 id1 id2
1 2 1
1 3 2
1 4 3
1 5 4
I've never been even half good in advanced database queries -- how would I go about achieving this?
If you have exactly 2 rows (with 2 values for id1) for every different value of id2, you can use this:
SELECT MIN(id1) AS id1_a
, MAX(id1) AS id1_b
, id2
FROM tableX
GROUP BY id2 ;
You could try using
SELECT id2, GROUP_CONCAT(id1) FROM your_table
GROUP BY id2
This way you have, for each id2 value, a column with all id1 values comma separated.
Take a look at GROUP_CONCAT syntax.
This might not be the perfect solution but in your case, it should work. This is a trick I used.
select id1, (sum(id1) - id1 ) as nID1, id2 from table_name group by id2
Hope it works.
Ujjwal
I have a table with the following design:
ID OPT_1 OPT_2 OPT_3 A_ID
1 3 4 3 1
2 5 2 1 1
3 1 2 2 1
I want to select all OPT's for A_ID 1, but when I run the query, I don't know the A_ID, I only know the OPT's.
So how can I insert a variable to get A_ID for the options I want?
for instance:
SELECT * FROM table
WHERE ((OPT_1 = 1 OR OPT_1 = 5)
AND (OPT_2 = 4 OR OPT_2 = 2)
AND (OPT_3 = 3 OR OPT_3 = 2)
AND A_ID = $X=$X)
Thanks,
Possibly best way to do it is to split it up a bit.
SELECT *
FROM table
WHERE A_ID in (
SELECT A_ID
FROM table
WHERE (OPT_1 = 1 OR OPT_1 = 5)
union
SELECT A_ID
FROM table
WHERE (OPT_2 = 4 OR OPT_2 = 2)
)