Check if values exist in two MySQL tables - php

Here is the table number (1)
ID NAME
1 Adam
2 Max
3 Alex
and here is the table number (2)
ID USER_ID AGE
1 1
2 2 21
3 3 23
i want to check the age of all users from table number (2) , also if the filed empty will echo empty
using php ... i've tried foreach loop and it didn't worked so help me please and sorry for my bad english
best regards .

if you have your age column as varchar then use this to check between empty strings or null
SELECT name, if(age ='' or age is null, 'empty',age) age
FROM table1
JOIN table2
ON table1.id = table2.user_id
DEMO

You could join the tables and use the coalesce function to replace null with your custom string (empty):
SELECT username, COALESCE(age, 'empty')
FROM table1
JOIN table2 ON table1.id = table2.user_id

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;

Update two table fields value using MySQL query

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)

Mysql select from table by comparing value in another table

I have two tables first and second.
first Table:
ID NAME ADDRESS
1 test testing address
2 test1 testing address
3 test2 testing address
4 test3 testing address
second Table:
T_ID Partner_id date
1 2 12345678
3 4 32164584
If input T_id is given. Then corresponding Partner_id is taken and it is compared with the ID from first table and corresponding row should be selected.
Can you please tell me.
In php I can write this with two queries but I want it to be in a single query.
Queries in php:
$q=mysqli_query($conn, "SELECT Partner_id from second where T_ID=1");
$qa=mysqli_fetch_array($q);
$w=mysqli_query($conn, "SELECT * from first where ID=$qa[0]");
But, how to combine this two statements?
The specified result can be returned using a query with a join operation.
SELECT f.*
FROM first f
JOIN second s
ON s.Partner_id = f.ID
WHERE s.T_ID=1
Note that there is a potential for this to return more rows that the original, if the first query returns more than one row. (That is, we don't assume that T_ID is unique in second, or that every row with a given T_ID value will have identical values for Partner_id.)
There are other query patterns that will return an equivalent result, but the join operation is the normative pattern.
try to use union ,see this this link
where The SQL UNION operator combines the result of two or more SELECT statements.
SELECT * FROM first WHERE ID IN( SELECT Parent_id FROM second WHERE T_ID = 1 )
or
SELECT * FROM first WHERE ID = ( SELECT Parent_id FROM second WHERE T_ID = 1 )
SELECT * FORM first_table AS f
JOIN second_table AS s
ON s.parent_id =f.id
WHERE s.T_ID = $id

pick 2 tables value for a specific column

table1: Proid email password verify
12345 john#xx.com xxxxxxx xxxx
45678 lee#xx.com xxxxxxx xxxx
// some more tables here
table2: Proid fname lname gender dofbirth
13456 rey aj male xxxxx
12345 john paul male xxxxx
47812 murray dj male xxxxx
45678 lee mah female xxxxx
Note this tables dont have duplicates of Proid
now here Proid is common for both the tables what i wanted is, to fetch an array simple as this
$result = mysql_query("SELECT table1.verify,table1.Email,table2.* FROM table1,table2 WHERE table2.Pro_ID='$pro_id' LIMIT 1");
$row = mysql_fetch_array($result, MYSQL_ASSOC);
// and I expect $row variable now has this values
$row['email'],$row['verify'],$row['fname'],row['lname'],row['table2*']
but no matter what its taking the first entry. How coud I fix this. is this the way to do it? Could anyone post or suggest me a good way to do it. Thanks
change this:
SELECT table1.verify,table1.Email,table2.* FROM table1,table2
WHERE table2.Pro_ID='$pro_id' LIMIT 1
to:
SELECT table1.verify,table1.Email,table2.* FROM table1
left join table2 on table1.Proid=table2.Proid
where table2.Proid in not null LIMIT 1
What happens with your query:
SELECT table1.verify, table1.Email, table2.*
//means: In each row include columns 'verify' and 'email'
// from table1 and all columns from table2.
FROM table1, table2
//means: Combine EACH row of table1 with EACH row of table2.
WHERE table2.Pro_ID='$pro_id'
//means: Fetch only rows where the 'pro_id' column
// of table2 has the specified value.
LIMIT 1
//means: Fetch only the first row.
So, what actually happens is that you combine each row of table1 with the row of table2 that has Pro_ID='$pro_id' (resulting in 4 rows for your example tables above) and then return the 1st row (which always contains the values of the 1st row of table1).
The problem is that there is no contsraint that forces the matching of rows from table1 and table2 only when their pro_id values are the same.
You can fix this in several ways.
1.) Add an additional condition in your WHERE clause:
...
WHERE table1.Pro_ID = table2.Pro_ID
AND table2.Pro_ID = '$pro_id'
...
2.) Joining the tables on column 'Pro_ID':
...
FROM table1 INNER JOIN table2
ON table1.Pro_ID = table2.Pro_ID
...
(More info on SQL joins.)

trying to output the correct value from SQL query from comparing a different table

I'm very new with SQL and need assistance on how I can accomplish this task using the correct query.
I have 2 tables that I need to use. Table "TB1" has:
id Name
1 bob
2 blow
3 joe
table "TB2" has:
compid property
1 bob
2 blow
I am trying to get which compid is missing in "TB2" and insert it from "TB1"
the query I am doing is:
SELECT id, name from TB1, TB2 where id <> compid
what I get is 2 ouputs of Id 1, and 2, and 3 outputs from id 3. by using php:
for($i=0;$i <= mysql_num_rows($comp)-1; $i++)
{
echo mysql_result($comp, $i, 0)."<br>";
}
and I expected the ouput 3 but instead got this:
1
1
2
2
3
3
3
I understand its comparing all the rows within the table but is there a way to achieve what I am looking for?
Thanks for your time.
You are performing an implicit Cartesian JOIN which results in every row against every other row. You need to specify what attribute JOINs the two tables.
Using implicit syntax (not recommended):
SELECT id, name
FROM TB1, TB2
WHERE id <> compid
AND TB1.Name = TB2.property <-- Column join
Using explicit syntax:
SELECT id, name
FROM TB1
JOIN TB2
ON TB2.property = TB1.Name <-- Column join
WHERE id <> compid
To accomplish your goal you would need something along the lines of:
SELECT TB1.id, TB1.name
FROM TB1
LEFT JOIN TB2
ON TB2.property = TB1.Name
WHERE TB2.compid IS NULL
See it in action
It's best practice to always alias the columns you select to prevent ambiguity.
To select it you can do:
SELECT *
FROM TB1
WHERE id NOT IN (
SELECT compid
FROM TB2
);

Categories