I'm making some assumptions about what you are specifically asking, if you were to supply a little more information it might help:
- Does
city_Mix already contain the field city2 or is that the column you wish to join from city_Data?
- Are you doing this in PHP or SQL?
- Is
city_Mix really a self-iteration of the city_Data table?
- Is the data you've presented representational of a database table, or a PHP data structure?
Once again, I am making some assumptions here, but I'll try to help based on what you've provided...
It appears that city_Mix matches #3 from my question list (so you want to take a list of items, and for each item in the list, map it to all other items on the list). If this assumption is correct, here is one way to approach the solution:
Using SQL
// You'll probably want to use an INNER JOIN to accomplish this quickly, something like the following
(SELECT 'cityName' FROM `city_Data` c INNER_JOIN `city_Data` m ON m.cityName != c.cityName)
Using PHP
FYI: I would recommend against this in favor of doing this with SQL, but wanted to provide it as "a way to do it" since you did not indicate which way you were approaching the solution.
<?php
$cityMixData = []; //Final data table as you've indicated you desire
//Connect to your database
$con = mysql_connect("localhost","inmoti6_myuser","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
// Define our SQL query
$query = "SELECT DISTINCT `cityName` FROM `city_Data` ORDER BY `cityId`";
// Execute the query
$cities = mysql_query($query);
$i = 1; // Iterator index
$cityMixData = [];
// Loop through the list of unique city names and map all other items accordingly
while( $city = mysql_fetch_array( $cities, MYSQL_ASSOC ) ) {
$tmpArr = $cities;
unset($tmpArr[$row[$i - 1]); // Remove self
$cityMap = array_values($tmpArr); // reindex array
$mixRow = array($mixId => $i, $city1 => $city['cityName'], $city2 => $cityMap); // Create a row with auto-increment ID, city1 === unique city name, city2 === list of unique cities sans-city1
$cityMixData[] = $mixRow; // Add row to cityMixData which is multi-dimensional array
$i++; // Increment iterator
}
// Connect to DB
// Create city_Mix table if not exist
// Appropriately generate SQL based on $cityMixData structure, might get messy
// Save to DB
?>
If you answer the questions I posed above, I will be able to help more.