Jump to content


Photo

very simple question (problem with select)


  • Please log in to reply
11 replies to this topic

#1 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 27 August 2006 - 01:34 PM

hi guys, I know that it sounds like funny, but I cann't select multiple tables from db. I've tryed some tutorials but... I have errors, if it won't take your time, please look at it and tell me what's the problem. here are some examples:

$query = "SELECT * FROM test OR test2 WHERE test LIKE '%$trimmed%' ORDER BY test";
$query = "SELECT * FROM test AND test2 WHERE test LIKE '%$trimmed%' ORDER BY test";
$query = "SELECT * FROM test, test2 WHERE test LIKE '%$trimmed%' ORDER BY test";

Thanks for you time
I don't need your script, I'll try to write it myself

#2 Techbot

Techbot
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 27 August 2006 - 03:23 PM

I'm a noobie so excuse if I'm wrong but  it might be because your fieldname is same as tablename.

$query=  "SELECT * FROM test,test2 where fieldname LIKE '%$trimmed%' ORDER BY fieldname " ;






either way consider your post bumped :-\

#3 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 27 August 2006 - 03:44 PM

I've tryed to change the fieldname but... Here is the eror:

$query = "SELECT * FROM test, test2 WHERE mes, mes LIKE '%$trimmed%' ORDER BY mes";

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 ' mes LIKE '%fun%' ORDER BY mes' at line 1
I don't need your script, I'll try to write it myself

#4 Techbot

Techbot
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 27 August 2006 - 03:47 PM

$query = "SELECT * FROM test, test2 WHERE  mes LIKE '%$trimmed%' ORDER BY mes";


ie not mes,mes

assuming the field mes is in both tables.

#5 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 27 August 2006 - 03:51 PM

yes u are right mes is in both tables, any ideas how to fix that? thanks for your help.
I don't need your script, I'll try to write it myself

#6 Techbot

Techbot
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 27 August 2006 - 03:58 PM

this seems to be the same issue as

http://www.phpfreaks...c,105805.0.html

post just above yours

$query = "SELECT * FROM 'test', 'test2' WHERE  'mes' LIKE '%$trimmed%' ORDER BY mes";

or to join both tables use


Select * FROM 'test1' NATURAL JOIN 'test2 WHERE 'mess' LIKE '%$trimmed%' ORDER BY mes" ;


#7 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 27 August 2006 - 05:17 PM

I've got this:

Column: 'mes' in where clause is ambiguous

what does it mean?
I don't need your script, I'll try to write it myself

#8 Techbot

Techbot
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 27 August 2006 - 06:21 PM

Ah sorry (I've only been at this a few days)

it means because there are two fieldnames 'mes' it doesn't know which one to use to do the comparison.

While you wait for someone more experienced than me to post, I suggest you stick with the NATUAL JOIN method.

#9 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 27 August 2006 - 06:29 PM

Ah sorry (I've only been at this a few days)

it means because there are two fieldnames 'mes' it doesn't know which one to use to do the comparison.

While you wait for someone more experienced than me to post, I suggest you stick with the NATUAL JOIN method.


Thank u very much for your attention!
I don't need your script, I'll try to write it myself

#10 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 27 August 2006 - 06:43 PM

how can I run query for each table? thanks again.
I don't need your script, I'll try to write it myself

#11 Gruzin

Gruzin
  • Members
  • PipPipPip
  • Advanced Member
  • 448 posts
  • LocationGeorgia

Posted 27 August 2006 - 07:09 PM

Here, this code works for me but I've got another problem: ORDER BY doesn't work with it...
Another question: can I limit the text displayed from row?

$query = "SELECT * FROM test WHERE mes LIKE '%$trimmed%' UNION 
SELECT * FROM test2 WHERE mes LIKE '%$trimmed%'";

I don't need your script, I'll try to write it myself

#12 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 28 August 2006 - 10:17 PM

If you want to order the UNION, you'll have to wrap the whole thing in parentheses.. I don't know what you mean by "limit the text".
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users