Table a
Rowid Msgid Userid
1 3 55
2 3 56
3 3 57
4 4 55
5 4 56
Table Group
RowID GroupID UseriD
1 2 55
2 2 56
3 2 57
4 2 58
5 2 59
6 2 60
7 3 60
8 3 55
Here there is a table a and group table. Rowid primary keys
I want to insert rows into table a
This query will
Insert rows into table a i.e for msgid 3 there is already 55 56 57 so it has to insert only 58 59 60.
Insert into
table a (msgid,Userid)
values(#msgid,#userid)
where userid not in table a
where tbl_a.msgid=3
and tbl_group.groupid = 2
For Msgid 3 I want to check if there are any group members (groupID 2) associated in table a, if not then add a row to it.
ie add to table a
rowid Msgid Userid
6 3 58
7 3 59
8 3 60
So I wont insert the userid 55,56,57 because it is already in the table a for msgid 3. How to do a query for this scenario
Try below code
Insert IGNORE into
table a (msgid,Userid)
values(#msgid,#userid)
where userid not in table a
where tbl_a.msgid=3
and tbl_group.groupid = 2
I am considering that your insert query is correct...
Good Luck!!!
It's actually quite simple:
INSERT INTO TABLE_GROUP
SELECT * FROM TABLE_A
WHERE ... -- you can have or not have a where clause as you like
ON DUPLICATE KEY UPDATE;
Related
Table:
pk fk price
1 1 23
2 1 12
3 1 3
4 2 53
5 2 75
6 3 95
7 3 113
8 3 63
9 3 73
10 3 93
11 4 113
11 4 150
11 4 105
In the above table:
How to find out the lowest price based on it's common fk value.For example: lowest price for fk=1 is 3, for fk=2 is 53, for fk=3 is 63 and for fk=4 is 105.
I want a single SQL statement which could find lowest price for each common fk value.
You just want a basic aggregate per group.
select fk, min(price)
from your_table
group by fk;
sql query to count records between two tables
Table 1
sub_id
------
11
22
33
44
Table 2
txt_id sub_id
------------------
1 11
2 11
3 33
4 33
5 33
6 22
i want sql query to count sub_id from Table2
so the result will be
sub_id count
---------------
11 2
22 1
33 3
44 0
i have done it with php using loops but this way will be too slow
i prefer to execute it in 1 sql query
SELECT t1.sub_id, count(*)
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.sub_id = t2.sub_id
GROUP BY t1.sub_id;
Google sql aggregate functions.
I have a table that is having 3 columns
vid - auto increment column
video_id - containing numbers
a_id - containing junk numbers
The table looks like below.
Vid Video_id a_id
101 1 3
102 1 3
103 5 3
104 5 3
105 5 3
106 11 3
107 11 3
108 11 3
109 11 3
110 11 3
I want to update a_id column values based on video_id values. Values in a_id should be updated as below.ex: If there are five 11 digit in video_id then the value in a_id should be updated 1 through 5.
Vid Video_id a_id
101 1 1
102 1 2
103 5 1
104 5 2
105 5 3
106 11 1
107 11 2
108 11 3
109 11 4
110 11 5
You can use user defined variables to give rank for each video group and then join with your real table by your auto increment column and update a_id accordingly
update t
join (
SELECT
Vid,
#r:= CASE WHEN Video_id = #g THEN #r+1 ELSE #r:=1 END a_id
,#g:=Video_id
FROM t,(SELECT #r:=0,#g:=0) t1
ORDER BY Video_id
) t1
on(t.Vid =t1.Vid)
set t.a_id = t1.a_id
Demo
I would like to compare three columns in two different tables by joing through unique identifier.But my for single identifier there are multiple rows will be returned.
Example:
Table A
Identifier Flag1 Flag2 Flag3
1 56 36 46
1 89 65 33
1 56 89 22
1 11 89 65
Table B
Identifier Flag 1 Flag2 Flag3
1 56 36 46
1 89 65 33
1 56 89 22
1 10 89 65
Now i would like to compare these two tables based on Identifier 1 , can you please help me out if all the column values are matching i need update the flag.Thanks in advance
I think below code will help,
below code will update flag of table A for records whose column values are same as that of table b
update a
set someflag = 1
where exists
(
select * from B
where b.flag1 = a.flag1
and b.flag2 = a.flag2
and b.flag3 = a.flag3
)
I have a table say content table, having fields say
unitId | referenceId | contentId | masterContentId | sequence
Now I need to Insert on duplicate key update the values for sequence, referenceId fields with the values where masterContentId is say '10' and contentId is say '100', for the records where
unitId is say '500' and contentId is say '100'.
i.e.
rowId| unitId| referenceId | contentId | masterContentId | sequence|
-----+-------+-------------+-----------+-----------------+---------+
1 10 1 500 2 a
2 20 2 300 4 b
3 500 3 100 6 c
4 500 4 700 8 d
5 30 5 100 10 e
6 40 6 100 20 f
So, what I was trying to explain is that, I need to update row number 3, with the corresponding values of row 5.
How to do it, using Mysql Insert On duplicate Key Update