Please find picture to get more clarification on what I am asking
Click here
Let me tell you full description
I have two tables -
Table1 contains book_id and book_name
second table -
Table2 contains book_id, stock and cs_id
I want when someone choose cs_id so result displayed in terms of book_name and not book_id
I'm sorry for short description...
I have two tables in mysql database -
table1
SN ID Name
1 E-11 ELC
2 E-13 ELX
3 D-41 DME
and table2
SN ID CS_ID
1 E-11 C01
2 E-13 C01
3 D-41 C54
How to get result of all name from table1 using one cs_id from table2 using php.
Please help.
Thanks in advance!
Here what I'm using
$query = "SELECT table1.id, table2.name FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE table1.cs_id=$cs_id'
this is showing me 0 result.
Use TABLE LEFT JOIN
LIKE
select tb1.name,tb2.ID FROM TABLE1 as tb1 LEFT JOIN TABLE2 as tb2 ON tb1.ID=tb2.ID where tb2.CS_ID='$request_id';
I hope its work
Ok, so I can't seem to get this to work, but here is what I wan't to do. I have 2 tables one like this
Table1
----------
Row1
And this
Table2
----------
Row2
I want to get the data from Table1 where Row2 equals "thisvalue", in php. Any idea how I should go about doing this? Thanks.
You join the tables together
SELECT table1.* FROM table1 t1 JOIN table2 t2 ON t1.row1 = t2.row2
If you want only a row with a specific rw2-value, you can specify that in the WHERE clause
SELECT table1.* FROM table1 t1 JOIN table2 t2 ON t1.row1 = t2.row2 where t2.row='your value'
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) ;
Hi I do have an old table and new table with same index/data. TABLE1 and TABLE2
but TABLE1 has got more data than TABLE2. this was maintained by some one and I dont know how this happened. so my question is how do I compare these two table and find which data is TABLE2 missing?? there is almost 200000 datas there so manually doing is not possible...
in PHP:
http://us.php.net/manual/en/function.array-diff.php
in SQL:
SELECT * FROM TABLE1 WHERE id {NOT} IN ( SELECT id FROM TABLE2 )
depending on criteria of comparison
Solution without nested query:
select TABLE1.id from TABLE1 left join TABLE2 on TABLE1.id = TABLE2.id where TABLE2.id is null
Did you mean something like this:
SELECT * FROM TABLE1 t1 WHERE NOT EXISTS(SELECT * FROM TABLE2 WHERE t1.id == t2.id)
By same index I am hoping you mean they share a primary key?
SELECT * FROM TABLE1 WHERE username NOT IN (SELECT username FROM TABLE2)
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=?;