How do I select data form multiple tables


You need to use INNER JOIN, joining the tables by the appropriate foreign key:

SELECT
    *
FROM
    posts
INNER JOIN
    retweets
ON
    posts.post_id=retweets.retweet_post_id

Read here about the JOINs if necessary:

http://www.w3schools.com/sql/sql_join.asp

Or would it be more efficient to have it all done inside one table?

Read about the database normalization to learn, how to split the tables correctly. So you can avoid anomalies and decrease the amount of duplicated information.

I have created a Fiddle with what I think you are looking for. Using jQuery is very simple to do it.

$(document).ready(function(){

    $("#add").click(function(){
        $("#tables option:selected").each(function(){
           $(this).clone().appendTo("#tables_to_sync");
           $(this).remove();
        });
    });

    $("#remove").click(function(){
        $("#tables_to_sync option:selected").each(function(){
           $(this).clone().appendTo("#tables");
           $(this).remove();
        });
    });

});

This whole setup scares me. You should never be running queries as SYS. No one that has limited SQL knowledge should be going anywhere near SYS. Having tables in different schemas that contain information that you'd want to combine together in a single query implies a significant normalization problem. And having someone with limited SQL skills building dynamic SQL just adds to the set of issues.

That said, you can use dynamic SQL to construct the query you want. Something like this may be close to what you want.

CREATE OR REPLACE FUNCTION get_all_usernames
  RETURN sys_refcursor
IS
  l_sql VARCHAR2(4000);
  l_rc  SYS_REFCURSOR;
BEGIN
  FOR t IN (SELECT t.*,
                   rank() over (order by owner) rnk
              FROM dba_tables t
             WHERE table_name = 'C_USERNAMES'
             ORDER BY owner)
  LOOP
    IF( t.rnk = 1 )
    THEN
      l_sql := 'SELECT name, address FROM ' ||
               t.owner || '.c_usernames';
    ELSE 
      l_sql := l_sql ||
               ' UNION ALL ' ||
               'SELECT name, address FROM ' ||
               t.owner || '.c_usernames';
    END IF;
  END LOOP;

  OPEN l_rc 
   FOR l_sql;

  RETURN l_rc;
END;

You are creating the querys correctly but you aren't actually executing them.

So instead of doing <?php $sql = "INSERT INTO ..."; ?> you can do something like this

<?php $sql = mysql_query("INSERT INTO ..."); ?>

Setting the variable to a mysql query will execute the query so therefore this should run your querys now.

Try something like this

select p.pnumber, d.dnumber,e.lname, e.[address], e.bdate
from department D 
inner join project P on D.dnumber = P.dnum
inner join employee E on e.ssn = D.mgrssn
where p.location = 'STAFFORD'
Comments


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .