How to fetch data from two tables in sql - mysql

I have two tables:
This is table1:
product_id|product_desc|product_name|product_s_desc
This is table2:
product_price_id|product_id|product_price
Now I want to fetch data from these tables. product_id is same in both tables.
I want to fetch
product_s_desc
product_desc
product_name AND product_price from other table.
Please help me do this.

I'm assuming you have a field named product_price in your second table (you didn't list it):
SELECT t1.product_s_desc, t1.product_desc, t1.product_name, t2.product_price
FROM table1 t1
INNER JOIN table2 t2 ON t2.product_id = t1.product_id
You should check out the MySQL manual regarding JOINS, as this is a very basic part of writing SQL queries. You might also consider adding an index on table2 for the product_id field to make the query run faster.

Select * from table1 join table2 on table1.productid = table2.productid

SELECT t1.*,t2.product_price
FROM table1 t1,table2 t2
WHERE t1.product_id=t2.product_id

$sql = "SELECT Student.First_Name,Student.Last_name,Student.Mobile_No,Student.Email,Student.Institue,Student.DOB,Student.Gender
Address.Address_Line1,Address.City,Address.State,Address.Country,Address.Zip_code
FROM Student INNER JOIN Address
ON Student.Id=Address.Id;";

Related

How to join where duplicates are needed?

I have a table structure like this
Table1
PersonID, referrer
Table2
Event_A_ID, PersonID, status
Table3
Event_B_ID, PersonID, status
I want to get the event status for everyone from table one with referrer=X by joining all of the event tables like Event_A...K and checking for PersonID. Since people can appear in multiple events we can have cases like this
PersonID=1001, EventA_ID, referrer=X, status
PersonID=1001, EventB_ID, referrer=X, status
PersonID=1001, EventK_ID, referrer=X, status
PersonID=1002, ...
PersonID=1003, ...
But I am not sure how to JOIN all of the event tables since the IDs can be duplicates (and are desired).
I tried to make a separate select and use the in syntax
...
WHERE 1=1
AND PersonID IN (SELECT PersonID from table1 where referrer=X)
But then I realized everything before will be an aggregate of events.
Should I start with the SELECT from Table1? Select the valid IDs first and then select from the events after? If so, how do I JOIN? I feel like ideal looks like union of all the event tables and then select
You can use union all for row wise implementation of data or you can also use inner joins between tables if there is not much table events. This will represent data in column format.
SELECT * FROM tb2 AS t2 INNER JOIN tb3 t3 ON t2.personId = t3.personId INNER JOIN tb1 t1 ON t1.personId = t2.personId WHERE t1.refer='refer1'
There can be many other approach too depending on the number of tables you want to join.
You should also consider to use a better relations among your db tables as your current scenario will lead you to have as many tables as many events you have. This will create slowness in retrieving the data for multiple events.
use union all and then apply join
select a.person_id,a.referrer,b.eventID,b.PersonID,b.status from Table1 a inner join
(
select Event_A_ID as eventID, PersonID, status from Table2
union all
select Event_B_ID as eventID, PersonID, status from Table3
)b on a.personid=b.personid
You can do something like this with left joins:
SELECT t1.PersonID, t1.referrer,
t2.Event_A_ID, t2.status as status_a,
t3.Event_B_ID, t3.status as status_b
.
.
.
FROM table1 t1
LEFT JOIN table2 t2 ON t2.PersonID = t1.PersonID
LEFT JOIN table3 t3 ON t3.PersonID = t1.PersonID
.
.
.
WHERE t1.referrer = 'x'

how to join 2 table and select a particular column using a common column in both tables

I am having 2 different tables like table1 and table2 in that i have a common column customer_id and then i want to pick customer_loan from table1 and customer_name from table2 using the common column customer_id and join both and display in my page anyone help me
It is simple
SELECT table1.customer_loan,table2.customer_nam
FROM table1 INNER JOIN table2 ON (table1.customer_id =table2.customer_id)
try this
select table1.customer_loan,table2.customer_nam from table1 inner join table2 on table1.customer_id = table2.customer_id
You need a JOIN, like this:
SELECT table1.customer_loan, table2.customer_nam
FROM table1 JOIN table2
USING (customer_id)

join and select operation on two tables

i got two tables like
table1
=======
id primary
sku
price
vendor
table2
=====
id primary
sku
alt_sku
basically, my search term will be sku or alt_sku, and based on it, I want to select data from table1
what will be best approach to do this? should I create a third table to store relation between table1.sku and table2.sku /table2.alt_sku?? or how can I get data from table1
sorry, if this question is veryy foolish...I am trying to learn mysql, so for, I was on WP and never went to basics
You can try an inner join. Assuming TERM is the sku you are searching for.
SELECT
t1.*
FROM
table1 t1
inner join table2 t2 on (t1.sku = t2.sku)
WHERE
t1.sku = 'TERM'
or t2.alt_sku = 'TERM'
What exactly are you trying to do with Table2? Perhaps a better approach would be the following:
Table1
id
price
vendor
Table2
Table1Id
Sku
That way you can store a 1-N number of SKUs associated with Table1. If you only want 2 potential SKUs, then you could add a SkuType field to your Table2 - 1 for Primary and 2 for Alternate, but that may be going too deep for your needs...
And to query:
SELECT T.*
FROM Table1 T
JOIN Table2 T2 ON T.Id = T2.Table1Id
WHERE T2.Sku = 'SomeSku'
I think your table structure should be like that:
table1
=======
id primary
IdFromtable2
price
vendor
table2
=====
id primary
sku
alt_sku
Your Select Query will be like:
SELECT t1.*
FROM table1 t1
INNER JOIN table2 t2 ON t1.IdFromtable2 = t2.Id
WHERE t2.sku = #sku AND
t2.alt_sku = #alt_sku
Basically,The finger rule for table relationships is as following:
If the relations is one-to-many then you should save the PK from the many table as additional column in the one table.
If the relations is many-to-many you should create a third table (connecting table). This table will have both PK from both tables (for each tbl1 PK you will have all tbl2 PK's and vice versa).
That way you preventing duplicated records.

How to get data 3 tables?

can u please show me how to query 3 tables using *? thanks
You need to do a join on the tables in order to get the columns of all of them.
Warning: using * to get all columns is bad practice. You should qualify (name) all the columns you need.
Here is an example:
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t1.key2 = t2.key2
INNER JOIN table3 t3
ON t1.key3 = t3.key3
One way you probably don't like:
SELECT *
FROM table1, table2, table3
You have to give way more information.
This generates the Cartesian product of all the three tables.

How to get data from 2 mysql tables

what is the syntax if I want to utilize 2 or more tables for my mysql query.
Example, I'm going to fetch the idnumber from the 1st table and the religion on the 2nd table. And the query will return the combined version of those 2 tables showing only the religion and idnumber.
The code might look something like this , but it doesn't work:
select t1.IDNO, t1.LNAME t2.RELIGION from t1, t2 where t2.IDNO='03A57'
The SQL query would be as follows:
SELECT a.idnumber, b.religion FROM table1 a, table2 b
You can add conditions from both tables as well by doing the following:
SELECT a.idnumber, b.religion FROM table1 a, table2 b WHERE b.religion = 'Christian'
More information can be found in this thread: http://www.astahost.com/info.php/mysql-multiple-tables_t12815.html
SELECT t1.IDNO, t1.LNAME FROM t1 LEFT JOIN t2.RELIGION ON ( t2.IDNO = t1.IDNO )
(more or less)
The Join is the command that will link the two.
http://dev.mysql.com/doc/refman/5.0/en/join.html
The code below would do a cross-join.
SELECT tb1.id, tb2.religion FROM tb1 JOIN tb2 ON (tb1.religion_id = tb2.religion_id) WHERE t2.IDNO='03A57'
Again... see http://dev.mysql.com/doc/refman/5.0/en/join.html...