473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I need to join 4 table and perform select

11
Hi!
I have 4 tables (table1, table2, table3, table 4)
I need to do select * from all four table and get them sorted

all the tables have field named id, so I figured that's the field I should join all the tables with

let's say table1 has 5 records in it, and table2 has 3
when I do select I want that id from table2 starts from 6 and goes to 8

so...

when i do select the id would go from 1-8

and is there a way that i could get an id from table2(or i have to subtract id from first table(table1) from all tables....)

how do I select * from 4 tables? they all have field name id and I want to join tables using that field

i use php, so I have a variable named $t and I want to use similar query to this
$t="something";
SELECT * FROM table1 WHERE field1 LIKE '%$t%' OR field 2 LIKE '%$t%'

it works when I use four queries but I want to be able to join queries and get one result... and if possible I want the id field not to have duplicate values (as described, i want them to go from 1-number_of_id_from_table1 and then just add that number to id from table2 and so on...)

and if there's a way to get the table name, or id(i.e.from table2).... I don't know how to descibe it... but if my table1 has 3 records and table2 has 2 record, then the first id from table2 would have id=5


thx
Aug 12 '07 #1
17 24421
pbmods
5,821 Expert 4TB
Heya, bobo. Welcome to TSDN!

To join four tables:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         *
  3.     FROM
  4.     (
  5.             `Table1`
  6.         LEFT JOIN
  7.             `Table2`
  8.                 USING (`id`)
  9.         LEFT JOIN
  10.             `Table3`
  11.                 USING (`id`)
  12.         LEFT JOIN
  13.             `Table4`
  14.                 USING (`id`)
  15.     )
  16.  
http://dev.mysql.com/doc/refman/5.0/en/join.html
Aug 12 '07 #2
bobo420
11
i've tried that... and I think it works because I don't get nothing returned from mysql_error in php... but can I do mysql_fetch_array on such query or?

I've tried but I get :
supplied argument is not a valid MySQL result resource
Aug 12 '07 #3
pbmods
5,821 Expert 4TB
Heya, Bobo.

Let's see the code that runs the query.
Aug 12 '07 #4
bobo420
11
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table1 LEFT JOIN table2 USING ('id') LEFT JOIN table3 USING ('id') LEFT JOIN table4 USING ('id')
Aug 12 '07 #5
pbmods
5,821 Expert 4TB
Heya, Bobo.

Right. I meant the PHP code that calls mysql_query() and mysql_fetch_assoc() :)

PS. Thanks for using CODE tags!
Aug 12 '07 #6
bobo420
11
Expand|Select|Wrap|Line Numbers
  1. $do=("SELECT * FROM table1 LEFT JOIN table2 USING ('id') LEFT JOIN table3 USING ('id') LEFT JOIN table 4 USING ('id')")
Expand|Select|Wrap|Line Numbers
  1. $t=mysql_fetch_array($do)
Aug 12 '07 #7
pbmods
5,821 Expert 4TB
Heya, Bobo.

In between those two lines, insert this code:
Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query($do);
  2.  
Then change your last line to read:
Expand|Select|Wrap|Line Numbers
  1. $t = mysql_fetch_assoc($result);
  2.  
Aug 12 '07 #8
bobo420
11
sorry...
i missed to add mysql_query

here's my entire code for it, it displays only data from table 1

Expand|Select|Wrap|Line Numbers
  1. $do=mysql_query("SELECT * FROM table1 LEFT JOIN table2 USING (id) LEFT JOIN table3 USING (id) LEFT JOIN table4 USING (id)") or die(mysql_error());
  2. if ($tred=mysql_fetch_array($do)){
  3. do {
  4. $mid=$tred['id'];
  5. $lok=$tred['lokacija'];
  6. $opis=$tred['opis'];
  7. echo "$mid - $lok - $opis <br />";
  8. } while ($tred=mysql_fetch_array($do));
  9. }
all fields (id, lokacija,opis) are in all 4 tables
but all I get is ids from table1 and not the rest of the fields from table1 and no data from any other table
Aug 12 '07 #9
bobo420
11
after joining all four tables I need to get results matching keyword.
my query for one table is like this
Expand|Select|Wrap|Line Numbers
  1. $key="something";
  2. $do=mysql_query("SELECT * FROM table1 WHERE lokacija LIKE '%$key%'  OR opis LIKE '%$key%'");
and it works, but for just one table... can I do UNION of four queries? Would that be easier?

If so... how?

thx
Aug 12 '07 #10
pbmods
5,821 Expert 4TB
Heya, Bobo.

If your tables have columns with the same names, then it sounds like your best bet is to use the UNION keyword. Either that or change your database structure so that related data is organized together. But that is not always an option.

To combine queries:
Expand|Select|Wrap|Line Numbers
  1.     SELECT
  2.             *
  3.         FROM
  4.             `Table1`
  5. UNION
  6.     SELECT
  7.             *
  8.         FROM
  9.             `Table2`
  10.  
And so on.
Aug 12 '07 #11
bobo420
11
hi, thx for the responses...

can I use LIMIT on that query or how could I write that query

ie.
Expand|Select|Wrap|Line Numbers
  1. $do1=mysql_query("SELECT * FROM table1 WHERE opis LIKE '%$key%' OR lokacija LIKE '%$key%'");
  2. $do2=mysql_query("SELECT * FROM table2 WHERE opis LIKE '%$key%' OR lokacija LIKE '%$key%'");
  3. $do3=mysql_query("SELECT * FROM table3 WHERE opis LIKE '%$key%' OR lokacija LIKE '%$key%'");
  4. $do4=mysql_query("SELECT * FROM table4 WHERE opis LIKE '%$key%' OR lokacija LIKE '%$key%'");
  5.  
how could I make UNION using those queries and use i.e. LIMIT 0,5 or something?

and if not... could I just write
Expand|Select|Wrap|Line Numbers
  1. $do1="SELECT * FROM table1 WHERE opis LIKE '%$key%' OR lokacija LIKE '%$key%'";
  2.  
so, that it isn't an actual query, but when I put it all together then run mysql_query?

how?

thanks
Aug 12 '07 #12
bobo420
11
$trag is $key, I've changed the name of the var
Expand|Select|Wrap|Line Numbers
  1. $do=mysql_query("SELECT * FROM table1 WHERE opis LIKE '%$trag%' OR lokacija LIKE '%$trag%' UNION SELECT * FROM table2 WHERE opis LIKE '%$trag%' OR lokacija LIKE '%$trag%' UNION SELECT * FROM table3 WHERE opis LIKE '%$trag%' OR lokacija LIKE '%$trag%' SELECT * FROM table4 WHERE opis LIKE '%$trag%' OR lokacija LIKE '%$trag%'")or die(mysql_error());
  2.  
I get an error in the syntax...
Aug 12 '07 #13
pbmods
5,821 Expert 4TB
Heya, Bobo.

You can limit the results of your UNION by making the whole thing a subquery:
Expand|Select|Wrap|Line Numbers
  1. SELECT
  2.         *
  3.     FROM
  4.     (
  5.         (
  6.             SELECT
  7.                     *
  8.                 FROM
  9.                     `Table1`
  10.         )
  11.         UNION
  12.         (
  13.             SELECT
  14.                     *
  15.                 FROM
  16.                     `Table2`
  17.         )
  18.     )
  19.     LIMIT 0,5;
  20.  
Note that this will start cutting results from the `Table2` query before cutting results from `Table1`.

And if you're getting an error, you have to tell us WHAT error you're getting before we can help you :)
Aug 12 '07 #14
bobo420
11
like I said, I get an error in the syntax, it doesn't say where... but here's the code I get from my browser

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT * FROM table4 WHERE opis LIKE '%ivana%' OR lokacija LIKE '%ivana%'' at line 1
Aug 12 '07 #15
pbmods
5,821 Expert 4TB
Heya, Bobo.

You're missing a UNION in between two of your queries.
Aug 12 '07 #16
bobo420
11
i get ->Every derived table must have its own alias

when i limit everything, if I don't do that it works...


btw. if you ever come to Croatia, you have as much beer as you can drink. on me. thx, very much :)

and...

i use the same if/do/whil loop as posted above...
how could I get the name of the table specific id is in?
I've tried with mysql_tablename and mysql_field_table but I didn't get right results...
thx
Aug 12 '07 #17
pbmods
5,821 Expert 4TB
Heya, Bobo.

For a solution to the 'Every derived table must have its own alias' issue, check out this thread.

In terms of determining the name of the table for each query, try this:
Expand|Select|Wrap|Line Numbers
  1. (
  2.     SELECT
  3.             'Table1'
  4.                 AS `TableName`,
  5.             *
  6.         FROM
  7.             `Table1`
  8. )
  9. UNION
  10. (
  11.     SELECT
  12.             'Table2'
  13.                 AS `TableName`,
  14.             *
  15.         FROM
  16.             `Table2`
  17. )
  18.  
Aug 12 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: B. Fongo | last post by:
I learned MySQL last year without putting it into action; that is why I face trouble in formulating my queries. Were it a test, then you would have passed it, because your queries did help me...
4
by: DCM Fan | last post by:
{CREATE TABLEs and INSERTs follow...} Gents, I have a main table that is in ONE-MANY with many other tables. For example, if the main table is named A, there are these realtionships: A-->B...
2
by: Sebastian | last post by:
The following query needs about 2 minutes to complete (finding dupes) on a table of about 10000 addresses. Does anyone have an idea on how to speed this up ? Thanks in advance !!! Sebastian
2
by: rockyptc | last post by:
greetings. first, i apologize for asking an old question. it appears that i'm looking for a solution that was already given but it don't seem to fit my scenario. so thanks for putting up with me. ...
21
by: CSN | last post by:
I have a pretty simple select query that joins a table (p) with 125K rows with another table (pc) with almost one million rows: select p.* from product_categories pc inner join products p on...
6
by: Mike S | last post by:
Hi all, A (possibly dumb) question, but I've had no luck finding a definitive answer to it. Suppose I have two tables, Employees and Employers, which both have a column named "Id": Employees...
1
by: Pankaj | last post by:
I am new to myssql and am using a mysql database with the following tables Table Itinerary itinerary_id int(4) ship_id int(4) itinerary_title varchar(10) itinerary_duration varchar(10)...
3
by: Zeff | last post by:
Hi all, I have a relational database, where all info is kept in separate tables and just the id's from those tables are stored in one central table (tblMaster)... I want to perform a query, so...
4
by: johnfaulkner | last post by:
Hi, I am trying to perform a single select of data from 2 tables, table A and table B. Table B may have none, one or many corresponding rows. If table B has no corresponding rows then table B...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.