stewc Posted October 3, 2007 Share Posted October 3, 2007 I have a database in which there is a table 'content' which contains a field called 'address' This line of code pulls some info from the database $query= "SELECT COUNT(dest.outcode) as density, dest.outcode, dest.x, dest.y FROM content AS source, postcodes AS dest WHERE address = dest.outcode GROUP BY dest.outcode ASC"; 'address' is in the form '1 Street Name<br/ >Address Line1<br />Address Line2<br />Postcode' where postcode is the form 'AB12 3CD' What I want is to use only the first part of the postcode ie AB12 rather than the whole line. This code modifies the address to how I want it $query = "SELECT address FROM content; $result = @mysql_query ($query); while($row = @mysql_fetch_array($result)) { $addy = substr(strrchr($row['address'],">"),1,4); } but how do I incorporate this into the $query at the top? TIA Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/ Share on other sites More sharing options...
hemlata Posted October 3, 2007 Share Posted October 3, 2007 Hello, You can write the update query in the while loop. Take an example given below. while($row = @mysql_fetch_array($result)) { $addy = substr(strrchr($row['address'],">"),1,4); $query = mysql_query("UPDATE `content` SET `address` = '".$addy."' WHERE `address` = '".$row['address']."'") or die(mysql_error()); } Hope this could solve your issue. Regards, Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-360693 Share on other sites More sharing options...
stewc Posted October 3, 2007 Author Share Posted October 3, 2007 What I'm sayin is I have this line of code $query= "SELECT COUNT(dest.outcode) as density, dest.outcode, dest.x, dest.y FROM content AS source, postcodes AS dest WHERE address = dest.outcode GROUP BY dest.outcode ASC"; and want to apply this string modification substr(strrchr($row['address'],">"),1,4) But I cant do this can I $query= "SELECT COUNT(dest.outcode) as density, dest.outcode, dest.x, dest.y FROM content AS source, postcodes AS dest WHERE substr(strrchr($row['address'],">"),1,4) = dest.outcode GROUP BY dest.outcode ASC"; cos it throws up an error. Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-360709 Share on other sites More sharing options...
fenway Posted October 3, 2007 Share Posted October 3, 2007 MySQL has substring() function too.... Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-360803 Share on other sites More sharing options...
stewc Posted October 3, 2007 Author Share Posted October 3, 2007 So should this line $query= "SELECT COUNT(dest.outcode) as density, dest.outcode, dest.x, dest.y FROM content AS source, postcodes AS dest WHERE substr(strrchr($row['address'],">"),1,4) = dest.outcode GROUP BY dest.outcode ASC"; work ok or have I got to use substring()? Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-360844 Share on other sites More sharing options...
fenway Posted October 3, 2007 Share Posted October 3, 2007 That won't work because you have php code in your sql statement. You need to convert that php code into the equivalent sql code with SUBSTRING() and LOCATE(). Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-360928 Share on other sites More sharing options...
stewc Posted October 3, 2007 Author Share Posted October 3, 2007 Can someone explain what this line of code does? I sort of understand it but am confused by the dest.outcode (what is the purpose of the dot) and the variables seperated by the commas? $query_userCoords = "SELECT COUNT(dest.outcode) as density, dest.outcode, dest.x, dest.y FROM hwz_users AS source, hwz_postcodes AS dest WHERE source.outcode = dest.outcode GROUP BY dest.outcode ASC"; Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-361034 Share on other sites More sharing options...
fenway Posted October 3, 2007 Share Posted October 3, 2007 Varibles? No variables... it's TABLEALIAS.COLUMNNAME. Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-361044 Share on other sites More sharing options...
stewc Posted October 3, 2007 Author Share Posted October 3, 2007 So my table ALIAS is dest and COLUMNNAME is outcode? Column name makes sense as I have a column in a table called outcode but how do I know what table it refers to? Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-361106 Share on other sites More sharing options...
fenway Posted October 4, 2007 Share Posted October 4, 2007 TABLE AS ALIAS. hwz_postcodes AS dest Quote Link to comment https://forums.phpfreaks.com/topic/71640-modifying-an-sql-query-help/#findComment-362022 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.