0

My table in the DB

As you can see i have a table contain several IDs including Survey_survey ID and Store ID, i want to get all the Store IDs and their Survey_surveyIDs without duplicates.

Here is what i have tried

$getSurvey = mysqli_query($dbConnection , "SELECT DISTINCT Survey_surveyId FROM maintable");

while ($row = mysqli_fetch_array($getSurvey)) {

$getStoreIDS = mysqli_query($dbConnection , "SELECT DISTINCT Stores_storeID FROM maintable WHERE Survey_surveyId=".$row['Survey_surveyId']."");
Marc B
  • 356,200
  • 43
  • 426
  • 500
ShkarM
  • 43
  • 1
  • 7
  • 1
    don't do that. Unless you have a very explicit reason for doing so, anytime you have nested queries, and the "lower" query uses data from the "higher" one, you should rewrite them as a single `JOIN`ed query. – Marc B Jun 02 '16 at 15:47
  • You should use GROUP BY in your query with two values: http://stackoverflow.com/questions/2421388/using-group-by-on-multiple-columns – Nicolas P. Jun 02 '16 at 15:48
  • 1
    SELECT DISTINCT Stores_storeID, Survey_surveyId FROM maintable – Rick Jun 02 '16 at 15:54
  • @NicolasP. Er, no. – Strawberry Jun 02 '16 at 16:50

0 Answers0