sql - Trouble with MySQL query -
ok, first-off admit mysql syntax has never been strongest point. so, here am.
urls :
id url code =============================================== 1 http://www.google.com abcd 2 http://www.freetemplates4u.com efgh 3 ...
posts :
id title address =============================================== 1 title 1 http://mize.it/abcd 2 title 2 http://mize.it/efgh 3 ...
i want create query fetch following table
title url ======================================================= title 1 http://www.google.com title 2 http://www.freetemplates4u.com
in few words :
- take
url
-code
pairsurls
table - search
http://mize.it/
+code
inposts
table (inaddress
field) - combine final
title
,url
in result table.
i know has joins , concatenation, i'm lost.
sidenote : don't care neither current database's structure, nor performance issues. want transfer existing data, existing database (without having alter it), new website's database (under totally different format/structure).
you should change db-design, query have poor performance since mysql has full tablescan.
try adding code column in posts table hat has right value (populate on insert/update) , add index code (both tables).
now should able do.
select posts.title, urls.url posts inner join urls on post.code = urls.code
update:
if first part of url same, work
select post.title, urls.url posts inner join urls on post.adress = concat('http://mize.it/', urls.code)
Comments
Post a Comment