I have a table called tableA containing two columns, 1,2. I am trying to remove from column 1 a value depending on whats in the array. If that value exists twice, I want to set col1 as col1 = col1 - 2.
I could use UPDATE tableA SET col1 = col1 - 1 WHERE col2 IN (?); .. [arr] and be on my way, if WHERE IN () didnt ignore duplicate values on the array.
Let me also note that this isnt about primary or unique keys and there is no problem with duplicate values. Below is the query where X is where I need to fix.
UPDATE tableA SET col1 = col1 - X WHERE col2 IN (?); .. [arr]
I know I can loop for the arr and run a simple query like: UPDATE tableA SET col1 = col1 - 1 WHERE col2 = ? ; .. [arr[i]] each time but am trying to find a better way - if there is.
You could use UPDATE ... JOIN:
UPDATE tableA a
JOIN (SELECT id, COUNT(*) cnt
FROM (SELECT 1 AS id UNION ALL SELECT 1 UNION ALL SELECT 1
UNION ALL SELECT 2 UNION ALL SELECT 2) x -- here goes IN values
GROUP BY id
)s
ON a.col2 = s.id
SET a.col1 = a.col1 - cnt;
DBFiddle Demo
Related
Probably a big Title! sorry for that.. :(
Table 1
id col1 col2
1 10 one
2 11 two
3 10 three
Now, i would like to write a sql query to get distinct col1 from table1 which doesn't have three in col2. I need the output col1 - 11 only.
I tried like this select distinct col1 from table1 where col2 != 'three' but this gives the result as both 10 and 11. But for 10 it has corresponding row with three as col2 value. Kindly help me to find this.
Use group by and having.
select col1
from table1
group by col
having sum(case when col2='three' then 1 else 0 end)=0
If you are using MySQL, the having condition can be shortened to
select col1
from table1
group by col
having sum(col2='three')=0
as conditions are treated as booleans returning 1 for true and 0 for false.
using not in()
select distinct col1
from table1 t
where col1 not in (
select col1
from table1 i
where i.col2 = 'three'
)
or using not exists()
select distinct col1
from table1 t
where not exists (
select 1
from table1 i
where i.col1 = t.col1
and i.col2 = 'three'
)
Here is my sample table
Col1 Col2
A A
B B
A C
B D
C C
I want to be able to select distinct records where all rows have the same value in Col1 and Col2. So my answer should be
Col1 Col2
A A
B B
C C
Simply:
select distinct * from t where col1 = col2;
if both cols have null and you want to get that row too:
select distinct * from t where coalesce(col1, col2) is null or col1 = col2;
The query is already written in your request:
select distinct records where all rows have the same value in Col1 and Col2
SELECT DISTINCT *
FROM tbl
WHERE Col1 = Col2
How do I structure my query so I can count how many occurrences of a value in column 1 appears in column 2 and then store that result in a new column in the same table? (If a value is duplicated in the first column I still want to store the same value in the new column) For example if I had a table like this:
COL1 COL2
1 2
1 4
2 1
3 1
4 1
4 2
The resulting table will look like this:
COL1 COL2 COL3
1 2 3
1 4 3
2 1 2
3 1 0
4 1 1
4 2 1
Any help is appreciated I am new to sql! Thanks in advance!
Select
col1,
col2,
COALESCE(col3,0) as col3
FROM
mytable
LEFT JOIN
( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
And if you want the update (thanks Thorsten Kettner ) :
UPDATE mytable
LEFT JOIN ( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
SET mytable.col3 = COALESCE(temp.col3,0)
You can easily count on-the-fly. Don't store this redundantly. This would only cause problems later.
select
col1,
col2,
(
select count(*)
from mytable match
where match.col2 = mytable.col1
) as col3
from mytable;
If you think you must do it; here is the according UPDATE statement:
update mytable
set col3 =
(
select count(*)
from mytable match
where match.col2 = mytable.col1
);
To do that, you can try :
SELECT COL1, COL2, (SELECT COUNT(COL1) FROM `tablename` AS t2
WHERE t2.COL1 = t1.COL1) AS COL3 FROM `tablename` AS t1
Enjoy :)
i am trying to run a sql query which will not show distinct/duplicate values.
For example if using distinct option it would display only one unique result, but i would like to skip all detected distinct values i.e dont display distinct values
is it possible?
select col1 d from tb_col where col1 = '123';
col1
------
123
123
(2 rows)
select distinct col1 d from tb_col where col1 = '123';
col1
------
123
(1 row)
SELECT col1
FROM tb_col
GROUP BY col1
HAVING count(*) = 1
Not showing duplicates at all:
SELECT col1 AS d
FROM tb_col
GROUP BY col1
HAVING COUNT(*) = 1 --- or perhaps HAVING COUNT(*) > 1
--- it's not clear what you want.
select col1
from tb_col
group by col1
having count(*) < 2
Try with DISTINCT it will works!
SELECT DISTINCT(col1) as d from tb_col where col1 = '123';
I have a table name T1 having only one Column name Col1 having rows –
Col1
a
b
c
And another table name T2 also having only one Column name Col1 having rows –
Col1
x
y
z
Now I want record like
Col1--Col2
a------x
b------y
c------z
I am using mysql.
Thanks in advance!!
create table T1(col1 varchar(10));
insert T1 values ('a'),('b'),('c');
create table T2(col2 varchar(10));
insert T2 values ('x'),('y'),('z');
select A.col1, B.col2 from
(select #r:=#r+1 rownum, col1 from (select #r:=0) initvar, T1) A,
(select #s:=#s+1 rownum, col2 from (select #s:=0) initvar, T2) B
where A.rownum=B.rownum
Because there is no ORDER BY clause, you are depending on luck and convention for the row numbering to be according to the order inserted. It may not always be the case.
In your example, if you want to join the tables to get row results like this:
Row 1 - A,X
Row 2 - B,Y
Row 3 - C,Z
..then you will have to add a common field that you can JOIN the two tables on.
If you want to be able to return results from both tables like this:
Row 1 - A
Row 2 - B
Row 3 - C
Row 4 - X
Row 5 - Y
Row 6 - Z
.. then you will need to use a UNION:
(SELECT Col1 FROM T1) UNION (SELECT Col1 FROM T2)