Get the values from 2nd and 3rd table for the matched values in 1st table

niko

I am a jQuery developer and I do not have good idea on how to retrieve data from tables. I am just trying to learn it.

I have a special case to deal with. Consider three tables

tb1 
-----------------------------------------------
    pid    fname     lname       cid       eid
    12      jo         mo        16345     2345
    13      ko         ro        16324     2435

tb2
-----------------------------------------------
    cid     cname
   16345    amazed
   16324    bored

tb3
------------------------------------------------
    eid      ename
    2345      nolo
    21345     johny

I want to retrieve the data for the matching pid value. Say,

select * from tb1 where pid = 12 

It returns me pid, fname, lname, cid, eid

The cid value is 16345, Instead the number. I want that cid value to be matched to the tb2 and get the cname column value that is amazed and same with the eid that is the ename : nolo I want those numbers to be matched to tb2 and tb3 and get their string values.

I could do it using 3 MySQL queries

This is what I have tried

 $result = mysql_query("select * from tb1 where pid = 12");
 $row = mysql_fetch_array($result,MYSQL_ASSOC);
 $cname = mysql_query("select cname from tb2 where cid = '$row[cid]'");
 $x =  mysql_fetch_array($cname,MYSQL_ASSOC);
 $cname = mysql_query("select ename from tb3 where eid = '$row[eid]'"); 
 $y =  mysql_fetch_array($cname,MYSQL_ASSOC);

But I feel the code is not ideal. I guess there must be a way to retrieve in a single query but I am not sure what it must be. Maybe joins or sub-queries.

Florin

Use joins on 3 tables

SELECT tb1.*, tb2.cname, tb3.ename from tb1
JOIN tb2 ON tb1.cid = tb2.cid
JOIN tb3 ON tb1.eid = tb3.eid
WHERE tb1.pid = 12

Hope this helps

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the 3rd column in an array by using 1st and 2nd column values as index in Matlab

From Dev

MySql get Count, Value from 3rd Table with reference from 1st table

From Dev

Get UITextFields(1st Class) and Switches (2nd Class) values from other Class at runtime

From Dev

extracting values from html table using beautifulsoup4 (2nd row onwards, 1st and 6th column)

From Dev

how to get all values from 1st query and only some values from 2nd query?

From Dev

Awk comparing 3 values, 2nd file value between 1st file values with multiple column printout between both files to a 3rd

From Dev

Awk comparing 3 values, 2nd file value between 1st file values with multiple column printout between both files to a 3rd

From Dev

how to insert the same id from 1st table to the 2nd table

From Dev

JComboBox changes components only when going from 1st to 2nd (Not from 3rd to 2nd)

From Dev

Numpy indexing to get values of 2nd dim depending on 1st dim

From Dev

Unable to pass values from 1st page to 3rd page using query string

From Dev

Query 2nd table with value of the 1st

From Dev

uploading 1st and 2nd values of a map into the 1st and 2nd values of a vector pair

From Dev

Get List of 1st and 3rd Saturday of month if 2nd Saturday Appear in first 9 days

From Dev

How to get characters after 1st underscore, 2nd underscore, 3rd underscore in SQL Server

From Dev

Need way to Compare Two Tables by Date, sum values in table 2 by date, & output values to a 3rd table w/ totals of table 1

From Dev

Change the 2nd and 3rd pickerView acording to what row from the 1st picker is selected

From Dev

Transition from 3rd fragment tab to 2nd/1st fragment tab , breaks the app

From Dev

Change the 2nd and 3rd pickerView acording to what row from the 1st picker is selected

From Dev

1st, 2nd, 3rd and 4th partial derivatives of real data obtained from FFTW are incorrect

From Dev

SQLQuery - Counting Values For A Range Across 2 Values Grouped-By 3rd Value in A Single Table

From Dev

Insert data in 3rd table with the values inserted in 2 other table

From Dev

Doctrine Values from 2nd table not fetched when using join

From Dev

Display Data from Two Tables but the 2nd Table has more Data than the 1st One

From Dev

Is it possible to call the columns of the table1 in table3, going from 2nd table

From Dev

Getting null object reference If i start 2nd activity before 3rd activity (No error if I move from 1st to 3rd directly)

From Dev

override result of 2nd table with 1st table when boolean is TRUE in 2nd table

From Dev

How to ADD Table Row to the 2nd table exclusively without adding to the 1st table

From Dev

Find the average values in 2nd column for each distinct values in 1st column using Linux

Related Related

  1. 1

    How to get the 3rd column in an array by using 1st and 2nd column values as index in Matlab

  2. 2

    MySql get Count, Value from 3rd Table with reference from 1st table

  3. 3

    Get UITextFields(1st Class) and Switches (2nd Class) values from other Class at runtime

  4. 4

    extracting values from html table using beautifulsoup4 (2nd row onwards, 1st and 6th column)

  5. 5

    how to get all values from 1st query and only some values from 2nd query?

  6. 6

    Awk comparing 3 values, 2nd file value between 1st file values with multiple column printout between both files to a 3rd

  7. 7

    Awk comparing 3 values, 2nd file value between 1st file values with multiple column printout between both files to a 3rd

  8. 8

    how to insert the same id from 1st table to the 2nd table

  9. 9

    JComboBox changes components only when going from 1st to 2nd (Not from 3rd to 2nd)

  10. 10

    Numpy indexing to get values of 2nd dim depending on 1st dim

  11. 11

    Unable to pass values from 1st page to 3rd page using query string

  12. 12

    Query 2nd table with value of the 1st

  13. 13

    uploading 1st and 2nd values of a map into the 1st and 2nd values of a vector pair

  14. 14

    Get List of 1st and 3rd Saturday of month if 2nd Saturday Appear in first 9 days

  15. 15

    How to get characters after 1st underscore, 2nd underscore, 3rd underscore in SQL Server

  16. 16

    Need way to Compare Two Tables by Date, sum values in table 2 by date, & output values to a 3rd table w/ totals of table 1

  17. 17

    Change the 2nd and 3rd pickerView acording to what row from the 1st picker is selected

  18. 18

    Transition from 3rd fragment tab to 2nd/1st fragment tab , breaks the app

  19. 19

    Change the 2nd and 3rd pickerView acording to what row from the 1st picker is selected

  20. 20

    1st, 2nd, 3rd and 4th partial derivatives of real data obtained from FFTW are incorrect

  21. 21

    SQLQuery - Counting Values For A Range Across 2 Values Grouped-By 3rd Value in A Single Table

  22. 22

    Insert data in 3rd table with the values inserted in 2 other table

  23. 23

    Doctrine Values from 2nd table not fetched when using join

  24. 24

    Display Data from Two Tables but the 2nd Table has more Data than the 1st One

  25. 25

    Is it possible to call the columns of the table1 in table3, going from 2nd table

  26. 26

    Getting null object reference If i start 2nd activity before 3rd activity (No error if I move from 1st to 3rd directly)

  27. 27

    override result of 2nd table with 1st table when boolean is TRUE in 2nd table

  28. 28

    How to ADD Table Row to the 2nd table exclusively without adding to the 1st table

  29. 29

    Find the average values in 2nd column for each distinct values in 1st column using Linux

HotTag

Archive