get all rows having common parents - php

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)

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 retrieve data with 3 tables in mysql?

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.

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)

How to find required unique result

I have two table 'topic' and 'subcategory'
I am using this query--
Select * from `subcategory` as s
Inner join `topic` as f
WHERE s.`Subcategory_id` = f.`Subcategory_id
My result shows like
Category_id Subcategory_id Post_id time
2 2.3 4 2012-12-01
1 1.5 5 2013-01-20
1 1.3 6 2013-03-18
There's also other columns... but all I want is to select the latest Post_id and Subcategory_id of one Category_id ... that means here Category 1 has two Subcategory it will select only the latest(here 1.3) and same result all the time for all Category when database will grown larger. What will be the next query or how could I change the existing query to gain my desired result?
SELECT Post_Id, Subcategory_Id from subcategory as s, topic as t where
s.Subcategory_id = t.Subcategory_id and time = (
SELECT Max(time) from subcategory as s1, topic as t1 where
s1.Subcategory_id = t1.Subcategory_id and s1.Category_id = s.Category_id
);
Something like that, I think, will work.
SELECT TOP 1 ... ORDER BY whatever column determines "the latest"
e.g.
SELECT TOP 1 ... ORDER BY TIME DESCENDING
Or in case of mysql:
SELECT ... ORDER BY TIME DESCENDING LIMIT 1
Join your topic table with following query:
SELECT s.* FROM subcategory s
Inner JOIN (SELECT s1.Category_id,
MAX(s1.time1) AS max_time
FROM subcategory s1
GROUP BY s1.Category_id) y
ON y.Category_id = s.Category_id AND y.max_time = s.time1

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