3 Replies - 2336 Views - Last Post: 18 January 2012 - 09:28 AM Rate Topic: -----

#1 grydz3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 26-August 11

SELECT multiple table on mysql using LIKE .

Posted 18 January 2012 - 03:42 AM

hey guys have problem display all the details on mysql_query .how can i display all 3 table that have the same name using LIKE . if i have branch1 branch2 branch3 tables . i have a variable for $field that i need to match with my $find var.i know how to echo the info but i cant get the right SELECT statement. THANKS guys for reading this post. best of luck!

$result = mysql_query("SELECT * from branch1 branch2 branch3 WHERE branch1.$field branch2.$field branch3.$field LIKE '%$find%' ")



again thanks guys. ^^ really need help still a noob here . :)

Is This A Good Question/Topic? 0
  • +

Replies To: SELECT multiple table on mysql using LIKE .

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 5,972
  • Joined: 08-June 10

Re: SELECT multiple table on mysql using LIKE .

Posted 18 January 2012 - 04:46 AM

*
POPULAR

If you really have three tables named "branch1", "branch2", and "branch3", then you may have a more serious design issue. It's rarely a good idea to create multiple identical tables, where the name of the table contains a variable (like a number.) - There should only be one table called "branch", and in that table you have a column that stores the branch number.

That is, if you had these three tables:
branch1
+---------+-------------+
| some_id | some_value  |
+---------+-------------+
|       1 | this is a   |
|       2 | log table   |
|       3 | for branch1 |
+---------+-------------+

branch2
+---------+-------------+
| some_id | some_value  |
+---------+-------------+
|       1 | this is a   |
|       2 | log table   |
|       3 | for branch2 |
+---------+-------------+

branch3
+---------+-------------+
| some_id | some_value  |
+---------+-------------+
|       1 | this is a   |
|       2 | log table   |
|       3 | for branch3 |
+---------+-------------+


What you should be doing is this:
branch
+-----------+---------+-------------+
| branch_id | some_id | some_value  |
+-----------+---------+-------------+
|         1 |       1 | this is a   |
|         2 |       1 | this is a   |
|         3 |       1 | this is a   |
|         1 |       2 | log table   |
|         2 |       2 | log table   |
|         3 |       2 | log table   |
|         1 |       3 | for branch1 |
|         2 |       3 | for branch2 |
|         3 |       3 | for branch3 |
+-----------+---------+-------------+



This automatically solves your problem, because now there is only one column you need to search, and you can add as many branches as you want without having to keep making new tables and altering all your SQL queries to match the new database structure.
Was This Post Helpful? 5
  • +
  • -

#3 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3509
  • View blog
  • Posts: 10,132
  • Joined: 08-June 10

Re: SELECT multiple table on mysql using LIKE .

Posted 18 January 2012 - 04:53 AM

ok, that should quit with a bunch of errors. the tables should at least be separated by commas and the WHERE claus is also a single statement (the boolean operators are missing). check the manual for more information.

This post has been edited by Dormilich: 18 January 2012 - 04:54 AM

Was This Post Helpful? 1
  • +
  • -

#4 grydz3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 26-August 11

Re: SELECT multiple table on mysql using LIKE .

Posted 18 January 2012 - 09:28 AM

Thanks guys for the advice :) now i have more control and make query much shorter . again thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1