I have two tables which both have column acc_manager and would like to update both tables where the value for acc_manager is 34 to be 68 but I only wish for half of the values of my client table to be updated including related data in my company table.
In my example tables below, there are discrepancies such as a company having an account manager but a client of the same company may have a different account manager.
client
id name companyid acc_manager
1 name1 1 12
2 name2 2 23
3 name3 2 34
4 name4 3 34
5 name5 3 34
6 name6 4 34
7 name7 5 12
8 name8 4 34
9 name9 6 34
company
id name acc_manager
1 company1 12
2 company2 23
3 company3 34
4 company4 34
5 company5 12
6 company6 0
I've tried to update using UPDATE and LIMIT like so
UPDATE client, company
SET client.acc_manager = 68, company.acc_manager = 68
WHERE company.id = client.companyid
AND client.acc_manager = 34
AND company.acc_manager = 34
LIMIT 55
but this resulted in the following error
Incorrect usage of UPDATE and LIMIT
so I tried updating the values of just one table using both tables in the WHERE clause but this outputted another error.
Is there any way I can update both tables, updating half of the items that match a criteria in the client table, and any related rows in the company table?
Related
Let's say I write a SELECT statement in MYSQL(version MySQL 8.0) which fetches "n" records.
I want this "n" records be split approximately equally by 3 and additionally need to make sure that 10% of these n records are shared by the 3 parts that it was divided into.
Is it possible to do this in the select statement itself or do I need to write a function to perform this operation.
Example:
My select query fetches as below:
ID Name Age
1 ABC 22
2 XYZ 23
3 awe 22
4 wer 23
5 per 23
6 mnm 24
7 lmk 23
8 uyt 22
9 prt 23
10 ler 26
I now need it split into let's say 3 and 10% should be shared by the 3 parts that it has been split to (since it's 10 records, in this example 1 record should be shared by the three sets)
Set 1:
1 ABC 22
2 XYZ 23
3 awe 22
4 wer 23
Set 2:
4 wer 23
5 per 23
6 mnm 24
7 lmk 23
Set 3:
4 wer 23
8 uyt 22
9 prt 23
10 ler 26
I have a 2 Mysql tables like 1.Product(This table is hierarchical or recursive) and 2.Sales table in this will store sale id and product id. I need to show parent product with the sum of all hierarchical child product.
Product table having data like,
id name parent_id
1 Necklace NULL
2 Ring NULL
3 Earing NULL
4 Choker 1
5 Long Necklace 1
6 Short Necklace 1
7 2-Line 5
8 3-Line 5
9 Mango 5
10 Green 7
11 Red 7
12 White 7
13 Stud 3
Sales table will have data like,
id product_id no_of_pcs weight rate
1 10 5 40 35000
2 12 8 50 50000
3 9 2 20 25000
4 6 1 8 25000
5 13 2 16 22000
Now I'm trying the result as,
Product sale_pcs tot_pcs sale_wt sale_rate
1 Necklace 16 118 135000
3 Earing 2 16 22000
Now I'm using mysql query inside php recursive function by using product table. By using this even recursive function all the products need to check with sales table. This will happening performance issue. Could you please help any one to solve this issue whether this possible by doing in query itself
suppose this is my table structure of table user
id field_id user_id value
1 1 37 Lalit
4 2 37 Test
5 13 37 123
6 18 37 324
7 28 37 english
8 33 37 203
9 21 37 201
10 1 39 Mukesh
11 2 39 Test
12 13 39 523
13 18 39 245
14 28 39 French
15 33 39 278
16 21 39 2897
So I wnat to get the result to match the two or three values from the column value and want the result
I made query like
SELECT DISTINCT user_id FROM user where value =123 AND value=523;
But it is not working please give solution how we get the result
A value in a row, as per your example, cannot be both 123 and 523. You have to use OR
SELECT DISTINCT(user_id) FROM user WHERE value=123 OR value=523;
Alternatively you can also use IN clause
SELECT DISTINCT user_id
FROM user
WHERE value IN (123, 523);
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;
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;