How to update fields of one table using other table? - mysql

How can we update table 1 such that it will replace b field value of table 1 by that of table 2 where a field value are found same?
Suppose I have two tables
table 1
fields a b c
1 5 10
1 5 8
2 5 0
1 4 11
and
table 2
fields a b
1 6
1 7
2 5
1 4

I'm going on 6th form knowledge so I'll leave the code for you to do, but here's basically how I'd do it:Select all values from table 2For each value, select the rows from table 1 with the matching 'a' valueCount number of matching valuesIf it's over one, update table 1 set 'b' as the new value where 'a' matches
Edit: Oh, just realised, the 'a' values aren't unique, unless both tables have matching ID for each row, I'm not sure you can do it.

Related

SQL Order by a specific value of a one to many field

The context :
I have these three simple SQL tables : my database diagram
a Request has one or N Request_Field
Request_Field is a pivot table
Value inside the pivot table Request_Field can sometimes contain dates in string format
a Field has 0 or N Request_Field
These tables are part of a greater database and I cannot change them at the moment.
My question :
Is it possible to query multiple "Request" and order them by a specific value in "Request_Field" ?
Example :
Request table
id
1
2
3
Request_Field table
id
request_id
field_id
value
1
1
1
John
2
1
2
2021-01-01
3
2
1
Jane
4
2
2
2020-01-01
5
3
1
Jack
6
3
2
2022-01-01
Field table
id
label
1
name
2
desired_date
The SQL query I try to make : get all Request ordered by the desired_date record of the Field table (DESC).
Request desired results
id
3
1
2
Thank you

How to Update main table from second table with multiple same values in Mysql

I'm trying to update my main table by comparing it with another table that contains the same fields but have the daily values. The second table contains multiple records for same person too. (no primary key in this table)
My query executes fine but it stops when it finds the first match and doesn't scan rest of the table.
update crates as a, t23042019 as b set a.srsb=a.srsb+b.srsb, a.srsc=a.srsc+b.srsc, a.hfc=a.hfc+b.hfc, a.mix=a.mix+b.mix where a.name=b.name;
crates table
name srsb srsc hfc mix
hitesh 5 5 5 5
raman 2 3 4 1
t23042019 table
name srsb srsc hfc mix
raman 1 -2 0 1
hitesh 2 2 2 2
hitesh -5 0 0 -2
raman -1 0 0 0
Expected result after the query
crates table
name srsb srsc hfc mix
hitesh 2 7 7 5
raman 2 1 4 2
Query result
crates table
name srsb srsc hfc mix
hitesh 7 7 7 7
raman 3 1 4 2
Something like below:
UPDATE crates a
INNER JOIN (
SELECT name, SUM(srsb) AS srsb, SUM(srsc) AS srsc, SUM(hfc) AS hfc, SUM(mix) AS mix
FROM t23042019
GROUP BY name) s
ON a.name=s.name
SET a.srsb=a.srsb+s.srsb,
...

Resequence column numbers in MySQL

I have a table that has a column with numbers in a sequence, but for some numbers they can be in groups with several of the same number. Then when the user deletes out a group, there can be a gap in the numbers.
What I want to do is re-sequence that column so it starts at 1, then proceeds up to the last column. So for example, the table column named Item could look like this:
1
1
2
3
5
5
7
8
8
and I want it to convert to this:
1
1
2
3
4
4
5
6
6
Is there a way to do this in MySQL?
I wouldn't encourage to do this, but you can re-number your values after each update
set #cnt = 0;
update test t set t.number=#cnt:=#cnt+1;
Update: this will increment the number field by one

Querying in SQL: one to many relationships among fields

I have a table structure as follows - there are two columns A and B. For one value of column A, there can be many values of column B (Corresponding to multiple rows). I want to query SQL in a manner that I get all the values of column A for which corresponding to one particular value of column A, column B does not take a particular value. eg:
A B
1 1
1 2
2 1
2 3
2 4
3 2
3 4
3 5
If I don't want column B to have the value 3 for a particular value of column A, the query should return the following on above data
A
1
3
I cannot figure out how to write such a query and searching manually is too time consuming. Please help me write the query. Thanks in advance.
you question is not very clear. I understand that you want something like
SELECT DISTINCT A FROM table WHERE A NOT IN (SELECT A FROM table WHERE B = 3)

Display result for mysql query

table A
id name group_id email
1 a 1 a#g.com
2 b 3,4 b#g.com
3 c 1,3,4 c#g.com
4 d 2,5,1 d#g.com
table b
id user user_group_id
1 x 1,3
The table structure is as above
OUTPUT: if i search for user_group_id (from table B) for 1,3 in table A then i should get 4 email addresses i.e a#g.com,b#g.com,c#g.com,d#g.com. Since 1 is present in 3 rows in table A and 3 is present in 2 rows.
While I don't exactly understand what your problem is, I have the impression that your table structure is not properly normalized.
Your table b should have two entries:
id user user_group_id
1 x 1
2 x 3
In this case, you can properly join your tables and get all answers when querying for a certain user name.