Update two table fields value using MySQL query - php

I have two MySQL table with contain values like
table1
id email_id
===========
1 abc#xyz.com
2 bbc#xy.com
3 gty#xyz.com
4 iut#xyz.com
5 tyk#xy.com
table2
id name email_id
===========
1 abc abc#xy.com
2 bbc bbc#xy.com
3 gty gty#xy.com
4 iut iut#xy.com
5 tyk tyk#xy.com
6 tyr tyr#xy.com
7 iut iut#xy.com
Result
table2
id name email_id
===========
1 abc abc#xyz.com
2 bbc bbc#xy.com
3 gty gty#xyz.com
4 iut iut#xyz.com
5 tyk tyk#xy.com
6 tyr tyr#xy.com
7 iut iut#xyz.com
You can see in my first table is a combination of #xy.com and #xyz.com. So i need to change all #xy.com to #xyz.com in table2 whether if table1 is same #xyz.com.
Example: case1- in table1 abc#xyz.com is available and its #xyz format, so in table2 i needs to change it as abc#xyz.com
case2- in table1 bbc#xy.com is available and its in #xy format, So in table2 i need not change bbc#xyz.com. i can leave it as it is.
i think you understood my issue and please give me a mysql query for solve it.Thanks in advance

You can join together table1 and table2 and then compare the length of the email addresses for each id. Since you want to conditionally replace the #xy format with the #xyz format, you can simply choose the longer email address to retain in the query.
SELECT t2.id, t2.name,
CASE WHEN CHAR_LENGTH(t2.email_id) > CHAR_LENGTH(COALESCE(t1.email_id, ''))
THEN t2.email_id ELSE t1.email_id END AS email_id
FROM table2 t2 LEFT JOIN table1 t1 ON t2.id = t1.id

i had written in sql server
create table #stack_table1(id int identity(1,1),
email_id varchar(1000))
create table #stack_table2(id int ,
name varchar(50),
email_id varchar(1000)
)
insert into #stack_table1(email_id)
(select ('abc#xyz.com')
union
select ('bbc#xy.com')
union
select ('gty#xyz.com')
union
select ('iut#xyz.com')
union
select ('tyk#xy.com')
)
insert into #stack_table2(id,name,email_id)
(
select 1,'abc','abc#xy.com'
union
select 2,'bbc','bbc#xy.com'
union
select 3,'gty','gty#xy.com'
union
select 4,'iut','iut#xy.com'
union
select 5,'tyk','tyk#xy.com'
union
select 6,'tyr','tyr#xy.com'
union
select 7,'iut','iut#xy.com'
)
select * from #stack_table1
select * from #stack_table2
tables were created
update statement will be as follows
UPDATE st2
SET st2.email_id = st1.email_id
FROM #stack_table1 st1
JOIN #stack_table2 st2 ON st2.NAME = substring(st1.email_id, 1, CHARINDEX('#', st1.email_id, 1) - 1)
if only select query needed please use this
SELECT st2.id
,st2.NAME
,coalesce(st1.email_id, st2.email_id)
FROM #stack_table1 st1
RIGHT JOIN #stack_table2 st2 ON st2.NAME = substring(st1.email_id, 1, CHARINDEX('#', st1.email_id, 1) - 1)

Related

MySQL: Search for coincidences and differences in two tables

I need to find matchs between two tables, but also need to display when there is no match.
Table1: id, dni_number, name, business_id
Table2: id, dni, business_id
I need to form a table like this:
id
dni
name
business_id
is_match
1
12365478
John Doe
15451
1
1
22365478
Karen Doe
23451
0
is_match meaning 1: it found the dni in table1 and also in table2, 0 for not match
The query should have a where condition to find matchs from certain business_id
Any help will be much appreciated. Thanks in advance
SELECT
tblA.id,
1 as is_match
FROM tblA, tblB
WHERE tblA.id = tblB.id
UNION ALL
SELECT
tblA.id,
0 as is_match
FROM tblA, tblB
WHERE tblA.id != tblB.id
SELECT *, (table2.dnu = table1.dnu) AS is_match
FROM table1
LEFT JOIN table2 ON table1.business_id = table2.business_id
WHERE table1.business_id = xxx;

How to expand a comma delimited field from one table and copy it into multiple fields in a second table which uses the same unique id

I have table1 with a field named subjects which is comma delimited with a unique id for each item.
id1's subjects field contains this information: apple, banana, tomato, melon
id2's subjects field contains this information: pineapple,seed,jelly,fish,eel
Some fields contain as little as one subject and some contain as many as 8 subjects, all separated by commas.
I have table2 which contains 8 fields called cat1, cat2, cat3, cat4, cat5, cat6, cat7, cat8.
I am trying to use the below SQL statement to copy subjects field from table1, break it apart by commas and enter it into the 8 possible cat fields in table2 but I keep getting this error message: "A new statement was found, but no delimiter between it and the previous one. (near "SELECT" at position 68)"
. Please assist with correcting my code.
UPDATE table2 VALUES (cat1,cat2,cat3,cat4,cat5,cat6,cat7,cat8)
SELECT SUBSTRING_INDEX(subjects,',',1) AS cat1,
SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',2),',',-1) AS cat2,
SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',3),',',-1) AS cat3,
SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',4),',',-1) AS cat4,
SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',5),',',-1) AS cat5,
SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',6),',',-1) AS cat6,
SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',7),',',-1) AS cat7,
SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',8),',',-1) AS cat8 FROM
table1 WHERE id=table1.id;
Thanks.
This is the correct solution:
UPDATE table2 t2 JOIN table1 t1 ON t2.id = t1.id
SET
t2.cat1=SUBSTRING_INDEX(t1.subjects,',',1)
, t2.cat2=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',2),',',-1)
, t2.cat3=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',3),',',-1)
, t2.cat4=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',4),',',-1)
, t2.cat5=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',5),',',-1)
, t2.cat6=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',6),',',-1)
, t2.cat7=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',7),',',-1)
, t2.cat8=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',8),',',-1)
The problem is that if subject8 is blank, this JOIN copies subject7 and re-enters it into subject8, which gives tons of repeated subjects. Some items only have one subject and some have as many as 8 subjects.
Also, is there a way I could LIMIT the join to 10 rows so that I can test it out and make sure it works before I run the join on 18 million rows?
If table2 exists and has data to be matched from table1, then it is an update query you need.
UPDATE table2 t2 JOIN table1 t1 ON t2.id = t1.id
SET
t2.cat1=SUBSTRING_INDEX(t1.subjects,',',1)
, t2.cat2=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',2),',',-1)
, t2.cat3=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',3),',',-1)
, t2.cat4=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',4),',',-1)
, t2.cat5=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',5),',',-1)
, t2.cat6=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',6),',',-1)
, t2.cat7=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',7),',',-1)
, t2.cat8=SUBSTRING_INDEX(SUBSTRING_INDEX(t1.subjects,',',8),',',-1)
If table 2 does not have the data in it then, "insert into" does not the table for you in MySQL, You need to create the table then insert into it.
CREATE TABLE Table2
(id int,cat1 varchar(20), cat2 varchar(20), cat3 varchar(20), cat4 varchar(20), cat5 varchar(20), cat6 varchar(20), cat7 varchar(20), cat8 varchar(20))
;
INSERT INTO table2 (cat1,cat2,cat3,cat4,cat5,cat6,cat7,cat8)
SELECT id
, SUBSTRING_INDEX(subjects,',',1) AS cat1
, SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',2),',',-1) AS cat2
, SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',3),',',-1) AS cat3
, SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',4),',',-1) AS cat4
, SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',5),',',-1) AS cat5
, SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',6),',',-1) AS cat6
, SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',7),',',-1) AS cat7
, SUBSTRING_INDEX(SUBSTRING_INDEX(subjects,',',8),',',-1) AS cat8
FROM table1

get all rows having common parents

id parent_id child_id
1 1 1
2 2 2
3 2 2
4 1 1
I have a table from which i need to get the common values from data when i query it with id... for eg if id=2 and id=3 then return
id parent_id
2 2
3 2
i have tried this after hunting a lot through various examples :
SELECT ta.user_id,ta.interest_parent_id,ta.interest_child_id
FROM user_interest ta
WHERE ta.user_id=2 AND
(SELECT COUNT(*) FROM user_interest tb
WHERE ta.interest_parent_id=tb.interest_parent_id
AND tb.user_id=3 )>1
but it responds with only:
id parent_id
2 2
any help :( im using a mysql database with php/codeigniter to do the scripting
You can give it a try:
SELECT
tOne.id,
tOne.parent_id
FROM
(
SELECT
*
FROM user_interest A
WHERE A.id IN (2,3)
) tOne
INNER JOIN
(
SELECT
*
FROM user_interest A
WHERE A.id IN (2,3)
) tTwo
ON tOne.parent_id = tTwo.parent_id
AND tOne.id <> tTwo.id
ORDER BY tOne.parent_id;
SQL FIDDLE DEMO
Any suggestion towards optimization of the query is welcome.
EDIT: SQL FIDDLE
You can make a sub SELECT:
SELECT * FROM table WHERE Name IN (SELECT Name FROM table GROUP BY Name HAVING count(*) > 1)

Split record from one column to two column in MysQL or PHP

I have a problem to split data from attendance machine. The data source after export from it like this:
id name att
1 John 01/04/2015 7:59:00
1 John 01/04/2015 17:44:00
1 John 02/04/2015 7:50:00
1 John 02/04/2015 18:14
Where record (in and out) from fingerprint time save in one column. And I want to split the data to be like this:
id name in out
1 John 01/04/2015 7:59:00 01/04/2015 17:44:00
1 John 02/04/2015 7:50:00 02/04/2015 18:14:00
How to split those record into 2 column in MySQL or PHP (maybe)? Thank you.
Assuming there is only one in/out per day, it's as simple as self-joining on the date and greater time.
select t1.id, t1.name, t1.att as `in`, t2.att as `out`
from table1 t1
inner join table1 t2
on date(t1.att) = date(t2.att) and t1.id = t2.id
and t2.att > t1.att
sql fiddle demo
If you want to create a brand new table with this data, so you can get rid of the import, you just need to use this query as the input to create table, like so:
create table new_table
as
select t1.id, t1.name, t1.att as `in`, t2.att as `out`
from table1 t1
inner join table1 t2
on date(t1.att) = date(t2.att) and t1.id = t2.id
and t2.att > t1.att
You could try this one.
SELECT a.userID,a.name,min(a.att)as `in`,max(a.att) as `out`
FROM
(
SELECT userID,name,str_to_date(att,'%m/%d/%Y%T') as att,str_to_date(att,'%m/%d/%Y') as attd
FROM attendance
) as a
GROUP BY a.userID,a.attd

PHP SQL Has an issue

This is my table, I should fetch the MAX (id) of each status_id.
id status_id
10 1
11 1
12 2
13 2
14 2
15 4
16 4
So, I use this sql query, it works true and fetchs me all max ID.
select status_id, max(id) as max FROM `table`
where status_id in (1,2,3,4) group by status_id
This sql command fetchs me 3 MAX id using while.
11, 14, 16....
You see, there is not any suitable id to 3rd status_id. And if there is not any suitable id to 3rd status_id just mark it as zero. So I want that sql will bring these results:
11, 14, 0, 16
You can create a subquery which basically has all the ID's you need and have a left join against it.
SELECT a.status_ID,
IFNULL(MAX(b.id), 0) maxVal
FROM
(
SELECT 1 status_ID UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4
) a
LEFT JOIN `table` b ON a.status_id = b.status_id
GROUP BY a.status_id
SQLFiddle Demo
You can join with a temporary dummy table containing all ids and a stats_id value of 0:
SELECT dummy.status_id, COALESCE(MAX(id), 0) AS max
FROM (
SELECT 1 status_id
UNION SELECT 2 status_id
UNION SELECT 3 status_id
UNION SELECT 4 status_id
) dummy
LEFT JOIN `table`
ON dummy.status_id = table.status_id
GROUP BY dummy.status_id
But this does not scale and is a maintenance nightmare (you have to change the dummy-select in case a new status_id turns up). If you have a table containing ALL status_ids, replace the dummy-select with that table.

Categories