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;
Related
I am having 2 tables :
1.internal_employee_master
id employee_name unique_id
1 Noah ABCD
2 Liam ABCD
3 William ABCD
4 Benjamin ABCD
5 Jacob EFGH
2.external_employee_master
id name unique_id
1 Elijah ABCD
2 Ethan ABCD
3 Alexander EFGH
I am using UNION query to get both tables data into single table and display this data into html table.
select id
, employee_name
, unique_id
from internal_employee_master
where unique_id = 'ABCD'
union
select id
, employee_name
, unique_id
from external_employee_master
where unique_id = 'ABCD'
I want to store payslips of both employees into single table.
I have one table payslips with emp_id and emp_type columns.
I am storing data into payslips data like:
id pay_slip emp_id emp_type
1 Noah_payslip.pdf 1 internal
2 Liam_payslip.pdf 2 internal
3 Lia_payslip.pdf 1 External
as you can see in above table i am storing emp_id and emp_type of
both the tables in single columns each.
Now, i dont undestand how to split data of internal employee and
external employee from pay_slip table and show data in html table.
Currently, i am writing below sql joins to get employee_names of
internal and external employee tables but it doesnt work for me.
$id = $_GET['id];
SELECT ps.id,ps.pdf,ps.emp_id,ps.emp_type,external_employee.name as comemp,
internal_employee.comp_empl_name as comemp
FROM pay_slip as ps
INNER JOIN internal_employee_master as internal_employee ON internal_employee.comp_trad_id = ps.trade_id
INNER JOIN external_employee_master as external_employee ON external_employee.trad_id = ps.trade_id
where ps.is_deleted = 1 AND ps.id = '".$id."'"
Please help me to join query to get name and employee_name with respect to emp_type form pay_slip table.
How about using UNION again?
SELECT
ps.id,
ps.pdf,
ps.emp_id,
ps.emp_type,
external_employee.name AS comemp,
internal_employee.comp_empl_name AS comemp
FROM
pay_slip AS ps
INNER JOIN
internal_employee_master AS internal_employee ON internal_employee.comp_trad_id = ps.trade_id
WHERE
ps.is_deleted = 1 AND ps.id = '".$id."'
AND ps.type = 'internal'
UNION ALL
SELECT
ps.id,
ps.pdf,
ps.emp_id,
ps.emp_type,
external_employee.name AS comemp,
internal_employee.comp_empl_name AS comemp
FROM
pay_slip AS ps
INNER JOIN
external_employee_master AS external_employee ON external_employee.trad_id = ps.trade_id
WHERE
ps.is_deleted = 1 AND ps.id = '".$id."'
AND ps.type = 'external'
You could try this
SELECT ps.id, ps.pay_slip, ps.emp_type, COALESCE(i.employee_name, e.name) AS name
FROM payslips ps
LEFT JOIN internal_employee_master i ON i.id = ps.emp_id AND ps.emp_type = 'internal'
LEFT JOIN external_employee_master e ON e.id = ps.emp_id AND ps.emp_type = 'External'
AND ps.id = :ID
You can see this in action here http://sqlfiddle.com/#!9/53a195/7/0
I would mention that there are a number of issues in your included tables and queries. For example, irregular column names between tables (name vs. employee_name), you've missed the is_deleted column from your example schema, and you have capitalised and non-capitalised values in the emp_type column which is confusing.
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)
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)
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
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