Find difference in 2 tables / recordsets MySQL - mysql

I have 2 tables in MySQL DB (A and B)
Both have very similar structure since table B has records that are inserted with all data each time user changes his data in table A to have full history of changes. Column Lastupdate is INT type and keeps time of update in UNIX timestamp format
So let’s say tables have records like that:
TableA
Id User_id Name1 Name2 Lastname Lastupdate
1 12 John Alexander Smith 1405594757
2 27 Marry Ann Poppins 1405594877
3 51 Jean Claude VanFist 1405594677
Table B
Id User_id Name1 Name2 Lastname Lastupdate
1 12 John Alexander Smit 1405594747
2 27 Marry Ann Poppins 1405594757
3 51 Jean Claude VanFist 1405594757
1 12 John 1405594727
1 12 John Alex 1405594737
3 51 Jean VanFist 1405594757
For each record from table A I would like to see comparison with the most current record of table B (basing on Lastupdate) with the information which field was changed
So for user_id = 12 I would have something like
Id User_id Name1 Name2 Lastname Lastupdate
1 12 John Changed name2:Alexander Changed lastname:Smith 1405594757
I was trying something like that
SELECT if(u1.name1 <> u2.name1, CONCAT("Changed: ",u1.name1), u1.name1)
FROM tableA u1,tableB u2
WHERE u1.user_id = 7433 AND u2.user_id = 7433
But it seems not to be right way please advise

This should indicate what field values changed as well as what they were and what they became:
select a.user_id,
case when a.name1 <> b.name1 then concat(b.name1,' changed to ',a.name1) else null end as name1_chg,
case when a.name2 <> b.name2 then concat(b.name2,' changed to ',a.name2) else null end as name2_chg,
case when a.lastname <> b.lastname then concat(b.lastname,' changed to ',a.lastname) else null end as lastname_chg
from tablea a
join tableb b
on a.user_id = b.user_id
where b.lastupdate = (select max(x.lastupdate)
from tableb x
where x.lastupdate < a.lastupdate and x.user_id = a.user_id)

Related

write query using joins using condition

there arr two tables table1,table2.
table1:
number
type
name
1100
1
steve
1100
2
john
1100
2
jack
.....
and so on for different numbers
similiarly table2:
number
type
name
1100
1
abraham
1100
2
john
1100
2
jack
and so on for different numbers...
Expected output:join two tables based on equal number and equal type but on name condition it should print where there is mismatch.for example in above john had type '2' in table 1 and in table 2 also there is john but for type '1' it is mismatch as steve <> abraham.I am doing this query but the output contains rows with jack and john too:
number
type
name1
name2
1100
1
steve
abraham
1100
2
john
jack
1100
2
jack
john
here the number of rows in table 1 and table 2 are equal similiarly number of rows for each type in each table are also equal
select * from table1,table2 where table1.number=table2.number and table1.type=table2.type and table1.name <> table2.name
expected output:
number
type
name1
name2
1100
1
steve
abraham
As you can see above I have given the output which I got for my query(it contains jack,John pairs but I don't want them as for type '2' names are same in both tables)but if you see for type '1' names are different in both tables so I want to print them.if names are different in type'2' for both tables I want them too as output but here in example names are same in both tables for type '2'.Can anyone help me..?
I think this will do what you want.
SELECT table1.number
,table1.type
,table1.name
,table2.name
FROM table1
JOIN table2 ON
table2.number = table1.number AND
table2.type = table1.type AND
table1.name NOT IN (SELECT t2.name
FROM table2 t2
WHERE t2.number = table1.number AND
t2.type = table1.type)

How do I get MySQL to look at an ID column to check if another row with the same ID has a value in a different column?

So I have a table that looks like this
ID AccountType Name Individ
1 O Acme Company 00
2 P Joe Smith 00
1 John Doe 01
1 Steve Johnson 02
3 P Shirley Johnson 00
2 Jane Smith 01
1 Kevin Lastname 03
So, O stands for Organization, and P stands for personal. Subaccounts are mixed in the table with the primary accounts, and a subaccount of an account has the same ID, but a null accounttype. I need to find a way to find all subaccounts of one AccountType, excluding subaccounts of personal accounts. I tried an INNER JOIN, but I can't join a column on itself.
SELECT * FROM <schema.table>
INNER JOIN <schema.table> ON table.id = table.id
WHERE accounttype = ('B' OR 'F' OR 'O' OR 'S') AND
individ != '0'
GROUP BY 'id';
#underscore_d nailed my problem. I needed to assign aliases to the tables so that I could have the data relate to itself.
SELECT * FROM schema.table AS mbr2
INNER JOIN schema.table AS mbr1 ON mbr2.id = mbr1.id
WHERE mbr1.accounttype != 'P' AND
mbr1.accounttype IS NOT NULL AND
mbr2.individ != '00'
GROUP BY mbr1.id;

How can I retrieve multiple rows from a table and display the combined information in a single query?

I have used a dynamic form builder plugin in WordPress. When I input some data from this form data inserted in the table. The table looks like below.
id entry_id field_id slug value
1 1 fld_7213434 name abc
2 1 fld_5474822 father def
3 1 fld_4459896 gender Female
4 1 fld_6364274 village_name ijk
20 2 fld_7213434 name J Jahan
21 2 fld_5474822 father Md Ali
22 2 fld_4459896 gender Female
23 2 fld_6364274 village_name ijk
I need to show this table's data like below
S.N. name father gender village_name
1 abc def Female ijk
2 J Jahan Md Ali Female Adabor
How can I do that?
One idea is to join the table to itself:
SELECT
t1.entry_id `S.N.`,
t1.value name,
t2. value father,
t3.value gender,
t4.value village_name
FROM mytable t1
LEFT JOIN mytable t2 ON t2.entry_id = t1.entry_id AND t2.field_id = 'fld_5474822'
LEFT JOIN mytable t3 ON t3.entry_id = t1.entry_id AND t3.field_id = 'fld_4459896'
LEFT JOIN mytable t4 ON t4.entry_id = t1.entry_id AND t4.field_id = 'fld_6364274'
WHERE
t1.field_id = 'fld_7213434'
Of course, instead of mytable you have to use your correct table name that you have not mentioned in your question.
A join is the Cartesion product (Wikipedia) of all involved tables. After filtering the resulting set of rows for those with the correct combinations (in this case, entry_id and field_id must be filtered), you get exactly what you want.
For performance reasons, you might want to have an index on the columns entry_id and field_id, but you'll find out.

Join multiple columns on MySQL table

I am trying to join multiple columns on a MySQL table. I can't figure out how I can retrieve multiple columns into one single row, so I can display one row on my page.
table: employee_timesheet
id employee_id service_area_name sign_off_1 sign_off_2 sign_off_3 created_date status last_updated
1 2 London Rom Director NULL 2015-02-11 17:22:44 Submitted 2015-02-11 17:22:44
table (empty): employees_timesheet_sign_off_team_leaders
id employee_id timesheet_id sign_off_key team_leader_id
table: employees_timesheet_sign_off_roms
id employee_id timesheet_id rom_id sign_off_key
1 2 1 4 sign_off_1
2 2 1 5 sign_off_1
table: team_leaders
id first_name last_name email reports_to status date_added last_updated
2 Bob Test me#mydomain.com 4 Active 2015-02-03 12:50:25 2015-02-03 13:28:56
table: roms
id first_name last_name email status date_added last_updated
4 Bill Gates bill#mydomain123445678.com Active 2015-02-03 13:14:07 2015-02-03 13:28:40
5 Ben Morris ben#mydomain123445678.co.uk Active 2015-02-11 17:35:43 NULL
I need to join the above tables to get the following result:
ID: 1
Team Leader: (null)
Rom: Bill Gates,Ben Morris
Date: 2015-02-11 17:22:44
It would be very much appreciated if someone could help with this.
Thanks in advance.
For your use case, you need to use the GROUP_CONCAT function: http://www.percona.com/blog/2013/10/22/the-power-of-mysqls-group_concat/. The query would look something like:
SELECT et.id, CONCAT(l.first_name, ' ', l.last_name) 'team_leader', GROUP_CONCAT(DISTINCT CONCAT(r.first_name, ' ', r.last_name)), et.last_updated
FROM employee_timesheet et
INNER JOIN employees_timesheet_sign_off_roms etr ON et.id = etr.timesheet_id
INNER JOIN roms r ON etr.rom_id = r.id
LEFT JOIN employees_timesheet_sign_off_team_leaders etl ON et.id = etl.timesheet_id
LEFT JOIN team_leaders l ON etl.team_leader_id = l.id
GROUP BY et.id, team_leader, et.last_updated

Conditional Table change in MySQL Query

I tried to work out around this. But I think I am getting no where.
I have 3 tables:
This tale contains all questions and question types:
Table: Ref
id | type | info
==========================
1 SS Education
---------------------------
2 RB Gender
---------------------------
3 ST State
This table contains "options" for the questions in the above table 'Ref`
Table: ref_ans
id | q_id | answer_text
===========================
1 1 Masters
---------------------------
2 1 Bachelors
---------------------------
3 1 Undergrad
---------------------------
4 2 Male
---------------------------
5 2 Female
---------------------------
6 2 Dont want to disclose
This table contains states (type ="ST" in table Ref)
Table: us_states
id | answer_text
===========================
1 Alaska
---------------------------
2 Alabama
---------------------------
3 Arkansan
---------------------------
4 Arizona
---------------------------
5 Baltimore
---------------------------
etc
The result I want is:
ref.id, ref_ans.id, ref.answer_text / us_states.answer_text
*for a given ref.question_id *.
And the condition is: If the question_id, for which the answers requested is 'ST', it should pull the answers from us_states, otherwise, it should pull from ref-ans table.
I tried this. Obviously, this did not work:
SELECT ref.id,
CASE WHEN ref.type = 'ST' THEN
(SELECT ID, answer_text FROM us_states )
ELSE
(SELECT id, answer_text FROM ref_ans)
END
FROM ref
WHERE ref.ID = <id>
Any ideas?
Try:
SELECT a.id,
COALESCE(b.id, c.id) AS ans_id,
COALESCE(b.answer_text, c.answer_text) AS answer_text
FROM ref a
LEFT JOIN ref_ans b ON a.id = b.q_id
LEFT JOIN us_states c ON a.type = 'ST'
WHERE a.id = <id> AND (
(a.type <> 'ST' AND b.id IS NOT NULL) OR
(a.type = 'ST' AND c.id IS NOT NULL)
)
Assuming all the three tables are connected by id(1st column):
SELECT ref_ans.id, ref.id, if(ref.type='ST', us_states.answer_text, ref_ans.answer_text) as answer_text
FROM ref_ans
JOIN ref on ref.id=ref_ans.id
JOIN us_states on ref.id=us_states.id
WHERE ref_ans.q_id = <id>