Gruzin Posted August 27, 2006 Share Posted August 27, 2006 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 Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/ Share on other sites More sharing options...
Techbot Posted August 27, 2006 Share Posted August 27, 2006 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 :-\ Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81123 Share on other sites More sharing options...
Gruzin Posted August 27, 2006 Author Share Posted August 27, 2006 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] Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81131 Share on other sites More sharing options...
Techbot Posted August 27, 2006 Share Posted August 27, 2006 $query = "SELECT * FROM test, test2 WHERE mes LIKE '%$trimmed%' ORDER BY mes";ie not mes,mesassuming the field mes is in both tables. Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81132 Share on other sites More sharing options...
Gruzin Posted August 27, 2006 Author Share Posted August 27, 2006 yes u are right mes is in both tables, any ideas how to fix that? thanks for your help. Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81133 Share on other sites More sharing options...
Techbot Posted August 27, 2006 Share Posted August 27, 2006 this seems to be the same issue as http://www.phpfreaks.com/forums/index.php/topic,105805.0.htmlpost just above yours$query = "SELECT * FROM 'test', 'test2' WHERE 'mes' LIKE '%$trimmed%' ORDER BY mes";or to join both tables useSelect * FROM 'test1' NATURAL JOIN 'test2 WHERE 'mess' LIKE '%$trimmed%' ORDER BY mes" ; Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81137 Share on other sites More sharing options...
Gruzin Posted August 27, 2006 Author Share Posted August 27, 2006 I've got this:[color=red]Column: 'mes' in where clause is ambiguous[/color]what does it mean? Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81166 Share on other sites More sharing options...
Techbot Posted August 27, 2006 Share Posted August 27, 2006 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. Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81194 Share on other sites More sharing options...
Gruzin Posted August 27, 2006 Author Share Posted August 27, 2006 [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! Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81198 Share on other sites More sharing options...
Gruzin Posted August 27, 2006 Author Share Posted August 27, 2006 how can I run query for each table? thanks again. Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81201 Share on other sites More sharing options...
Gruzin Posted August 27, 2006 Author Share Posted August 27, 2006 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] Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81213 Share on other sites More sharing options...
fenway Posted August 28, 2006 Share Posted August 28, 2006 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". Link to comment https://forums.phpfreaks.com/topic/18800-very-simple-question-problem-with-select/#findComment-81877 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.