Jump to content

Modifying an SQL query help!


stewc

Recommended Posts

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

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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()?

Link to comment
Share on other sites

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";

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.