I have to three table I need to gather data from in a search process:
Commissions Table - Table1: [affiliate_id]
Affiliates Table - Table2: [id][user_id]
Profiles Table - Table3: [ID][NickName]
The search input I'll have is someone searched for a username. I need to return the data from table 1 where the affiliate_id matches the user_id of Table2, that is like the nickname that will be searched for.
I hope that makes sense :)
Try this:
"select table1.* from table1
inner join table2 on table2.user_id = table1.affiliate_id
inner join table3 on table3.id = table2.user_id
where table3.nickname like '%".mysql_real_escape_string($searchtext)."%'"
SELECT t1.*, t3.nickname FROM Table1 t1
JOIN Table2 t2 ON t2.id=t1.affiliate_id
JOIN Table3 t3 ON t2.user_id=t3.user_id
WHERE t2.user_id=?;
Related
I have three tables like
Table1
It's a blank table that have same columns as Table2.
Table2
It contains the actual data that needs to be copied in Table1.
id cola colb colc cold
1 hd dj dj dh
2 hf uy ug se
...
Table3
Before copying data from Table2 to Table1, first I need to verify that if the id is present in Table3 or not. In other words I just want to copy the rows from Table2 to Table1 whose id exist in Table3.
id col1 col2
1 xy zz
2 ys sh
One more thing Table2 & Table3 contains half millions of rows, so query must be feasible.
some one make this as a duplicate question
however the answer is
insert into Table1
select * from Table2 where Table1.id=Table2.id
Option 1 using subquery:
insert into Table1
select * from Table2 where id in (select id from Table3)
Option 2 using INNER JOIN:
insert into Table1
select * from Table2 INNER JOIN Table3 USING(id);
insert into Table1
select * from Table2 where id in (select id from Table3)
Above query will work if Number of columns in table1 and table2 are same
Else
insert into Table1 (column1,column2,.....)
select column1,column2... from Table2 where id in (select id from Table3)
Use join. It is faster.
insert into table1 (id, cola, colb, colc, cold)
select t2.id,t2.cola,t2.colb,t2.colc,t2.cold
from table2 t2
left join
table3 t3
on t2.id=t3.id
where t3.id is null
You can use EXISTS
Query
insert into Table1(id, cola, colb, colc, cold)
select id, cola, colb, colc, cold
from Table2 t
where exists(
select * from Table3
where id = t.id
);
OR with JOIN.
insert into id, cola, colb, colc, cold
select t1.id, t1.cola, t1.colb, t1.colc, t1.cold
from Table2 t1
join Table3 t2
on t1.id = t2.id;
I am new to php & mysql and I'm trying to make a script that gets the distance walked with the player's name. I can get the player's walked distance with his id, but the value for the player_id is in a different table.
It looks like this:
Table1: player_id | foot (walked distance)
Table2: name | player_id
So I want to use the name by the player_id in my table.
Code
You require a simple join.
SELECT Table1.foot, Table2.name
FROM Table1
INNER JOIN Table2
ON Table1.player_id = Table2.player_id;
You just need to join both these table.
Just try this code:
$query = "SELECT T1.*, T2.name
FROM table1 T1
LEFT JOIN table2 T2 ON T1.player_id = T2.player_id
ORDER BY T2.name ASC";
For more details of JOIN: Link
Let me know for more help.
You can use
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id"
If you want to order the player names in alphabetical order then you can additionally use order by clause
$query = "select t1.player_id, t2.name, t1.foot
from table1 t1
join table2 t2 on t1.player_id = t2.player_id
order by t2.name"
Use left join in mysql.
Suppose if you have two tables use this
SELECT T1.*,T2.walked distance
FROM table1 T1
LEFT JOIN table2 T2
ON T1.id=T2.player_id;
Click Here For more example
Here is my tables structure.
Table1 // it has 4 colums,that saves all the question posted by user
id|question|answer|postby|
Table2 //it has 2 colums, that saves all the signed up users
id|username|email|
Table3 //it saves that which question is read by which user.
id|reader_id|question_id|
NOTE:reader_id is the id of table 2.
Problem: We need to find out those questions that are not being read by a particular user.
select * from table1 where id not in (
select question_id from table3 where reader_id = [particular user id]
)
SELECT question FROM table1 where id NOT IN (SELECT question_id FROM table3 where reader_id IN (SELECT id FROM table2 where username='XXXX'))
Try this:
SELECT t1.*
FROM table1 t1
LEFT JOIN
(SELECT t3.*
FROM
table2 t2
JOIN table3 t3 ON t3.reader_id=t2.id
WHERE t3.reader_id=SOME USER)t ON t.question_id=t1.id
WHERE t3.id IS NULL;
Table1 Has Some data as Categories
Table2 Has Some data Which is Realated to table1 Categories
and the relation between two tables is cat_id from table1 and cat_ids from table2.
What I want is?
I need to display all fields in table1 and from table2 I need only the related content i.e id's present in cat_id(table1) and cat_ids(table2)
I am using a query like this select c.* ,cc.* from news_categories cc, news_content c where cc.cat_id = c.cat_ids group by cc.cat_id this gives only common data from table1 and table2.. i need common data and all categories from table1
can anyone help me?
You should use JOIN instead.
SELECT t1.*, GROUP_CONCAT(t2.content_id)
FROM table1 t1
LEFT JOIN table2 t2
ON t2.cat_ids = t1.cat_id
GROUP BY t1.cat_id
This is for all fields of both tables...
SELECT Table1.*, Table2.*
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids
This is for all fields of Table1 and Content field of Table2...
SELECT Table1.*, Table2.full_content
FROM Table1, Table2
WHERE Table1.cat_id = Table2.cat_ids
you need to use outer join and this case left join .. Have a look at Documentation
I have two tables Table1 and Table2 with some records
id is the common column in both tables and primarykey is set to this column in table1
There are many records in table1 and some of these records (not all) are updated into table2.
Now I want retrieve from table1 the records not updated into the table2.
For example in table1 there are records 1,2,3,4,5,6,7,8,9
And in table2 there are 3,4,7,9
Now How can I retrieve these records form table1 1,2,5,6 those not updated into table2
I wrote this query :
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME
FROM [Table1] INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id;
But the expected result not coming. This query lists all the records repeating each one record manytimes
Can any body give me solution to get the expected result.
select *
from table1
where table1.slip_no NOT IN (select id from table2)
Assuming name of common column is id
Or you can modify your query as
SELECT distinct (Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME)
FROM [Table1]
INNER JOIN Table2 ON Table1.SLIPNO <>Table2.id
A good reference on SQL joins
SELECT t1.*
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2 USING(id)
WHERE
t2.id IS NULL;
You can use the NOT IN operator on a subquery for table2.
Alternatively, use MINUS with two regular queries listing the ids in each table:
SELECT id FROM table1
MINUS
SELECT id FROM table2;
Try this
SELECT Table1.id, Table1.DATE, Table1.C_NAME, Table1.B_NAME FROM [Table1]
WHERE
NOT EXISTS (SELECT * from Table2 WHERE Table1.SLIPNO !=Table2.id );
You can use the following query
SELECT id FROM database1.table WHERE id NOT IN(SELECT id FROM database2.table)
SELECT child_table.id FROM child_table LEFT JOIN parent_table ON child_table.parent_id = parent_table.id WHERE parent_table.id IS NULL
This left join query returns all the records of the child_table when there is no match in the parent_table. When there is no match, all parent_table fields will be NULL.
inner join will not help. To get unmatched records I tried this:
SELECT
A.ID,A.DATE,A.NAME
FROM TABLE1 A
WHERE CONCAT(A.ID , A.DATE ,A.NAME)
NOT IN
(SELECT CONCAT(B.ID , B.DATE ,B.NAME) as X
from TABLE2 B) ;