You are here: Re: Concat in a join? « PHP Programming Language « IT news, forums, messages
Re: Concat in a join?

Posted by Rik on 12/12/06 02:07

GarryJones wrote:
> I think the following statement ....
>
> $ml_collect='SELECT *, DATE(CONCAT(field1, field2)) AS thedate FROM
> ml_lopp LEFT JOIN scfmforening ON (scfmforening.scfmnum =
> ml_lopp.scfmnum) LEFT JOIN ml_tidplats ON (ml_tidplats.loppnum =
> ml_lopp.loppnum) ORDER BY thedate';
>
> ....would work if "field1" and "field2" were in the table "ml_lopp"
>
> However, "field1" and "field2" are in the table "ml_tidplats" and
> accessed by "LEFT JOIN".
>
> So, how do I use "DATE(CONCAT(field1, field2))" in this case?

When all else fails, try to get them explicitly:
SELECT
`ml_lopp`.*,
DATE(CONCAT(`ml_tidplats`.`field1`, `ml_tidplats`.`field2`)) AS
'thedate'
FROM `ml_lopp`
LEFT JOIN `scfmforening`
ON `scfmforening`.`scfmnum` = `ml_lopp`.`scfmnum`
LEFT JOIN `ml_tidplats`
ON `ml_tidplats`.`loppnum` = `ml_lopp`.`loppnum`
ORDER BY `thedate`

MySQL is quite forgiving, and will mostly only require you to name the
tables explicitly if there's ambiguity (for instance field1 exists in both
scfmforening & tidplats. Telling it exactly what you want can do no harm
though. You might consider giving your tables an alias (like LEFT JOIN
`scfmforening` 's'), so you don't have to type out the whole tablename
again and again and again.

BTW: SELECT * is no good for production servers unless you're absolutely
sure you need ALL the fields. Your MySQL manual will explain you why in
great detail.
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация