Jump to content

Archived

This topic is now archived and is closed to further replies.

Gruzin

very simple question (problem with select)

Recommended Posts

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:

[code]
$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";
[/code]

Thanks for you time

Share this post


Link to post
Share on other sites
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 :-\

Share this post


Link to post
Share on other sites
I've tryed to change the fieldname but... Here is the eror:

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

[color=red]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[/color]

Share this post


Link to post
Share on other sites
$query = "SELECT * FROM test, test2 WHERE  mes LIKE '%$trimmed%' ORDER BY mes";


ie not mes,mes

assuming the field mes is in both tables.

Share this post


Link to post
Share on other sites
yes u are right mes is in both tables, any ideas how to fix that? thanks for your help.

Share this post


Link to post
Share on other sites
this seems to be the same issue as

http://www.phpfreaks.com/forums/index.php/topic,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" ;

Share this post


Link to post
Share on other sites
I've got this:

[color=red]Column: 'mes' in where clause is ambiguous[/color]

what does it mean?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote author=Techbot link=topic=105810.msg422883#msg422883 date=1156702871]
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.
[/quote]

Thank u very much for your attention!

Share this post


Link to post
Share on other sites
how can I run query for each table? thanks again.

Share this post


Link to post
Share on other sites
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?

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

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.