2

I am very new to php and mysql. I am trying to learn as I go and am stuck on this issue.

I have a many-to-many relationship. The tables are: users and state. Many companies in the users table operate in many states and each state has many different companies. Each company on the user table has a unique id that is stored on the state table. The state table then has a column for the state name that the company operates in. A new row is created in the state table for each state the company is in. That all seems to work ok.

I wrote the following php code to pull all the company id's from the state table for any given state ($state) and then display all the company names (pulled from the users table with the company id) that operate in the given state.

My problem is that I would like to order the list alphabetically of companies that operate in any given state. This code will not do that. Can anyone offer a better way to do this that will allow the list to be ordered (or that will just generally be more efficient and better)?

$state=$_GET['state'];
echo $state;
$aid=array();
$result=mysql_query("SELECT * FROM state WHERE state='$state'");
while($row=mysql_fetch_assoc($result)){
$v=$row['company_id'];
array_push($aid,$v);};

foreach($aid as $val){
$result1=mysql_query("SELECT * FROM users WHERE company_id='$val'");
$row1=mysql_fetch_assoc($result1);
$company=$row1['company'];
echo $company.'<br/>';};

The table structures are:

users

company_id
company
(other columns that are not relevant)

state

index
company_id
state
(no additional columns)
4
  • 2
    We're gonna need your table structure. Sep 6, 2012 at 19:15
  • What is the relationship of user to companies, or are you just using these terms interchangeably?
    – Mike Brant
    Sep 6, 2012 at 19:17
  • thatidiotguy, i just edited the question with the table structures.
    – JayK
    Sep 6, 2012 at 19:33
  • Mike, there are many users that are not companies and all the companies are users. The table with all users (including companies) is named 'users'.
    – JayK
    Sep 6, 2012 at 19:35

3 Answers 3

2

You typically would introduce a third table in a relational database to express a many-to-many relationship. The table could be users_states and have only to fields: user_id and state_id. So you tables should probably look like this:

You do the sort in your SQL. So the query might look like:

SELECT u.*, s.*
FROM users AS u
INNER JOIN users_states AS us ON u.user_id = us.user_id
INNER JOIN states AS s ON us.state_id = s.state_id
WHERE s.state_id = ?
ORDER BY u.name ASC

Here I am assuming that a field name is what you want to sort by and that you will filter on some value passed for state_id.

2
  • Mike, thanks for the help. I'm sure this is a good way to do it to. I went with the other answer because I am very new at programming and did not fully understand it.
    – JayK
    Sep 6, 2012 at 23:25
  • mike it a good answer, i have one also, get data from many to many relation like each user_id will show all related thing against that id for one time.
    – Ali Raza
    Aug 5, 2019 at 5:13
1

See this URL:-

Many-to-many relationship select and order by

Probably something similar to this:

SELECT
    a.person_id
FROM
    table AS a,
    table AS b
WHERE
    a.person_id = b.person_id AND
    a.favorite_id = 1 AND
    b.favorite_id = 2
ORDER BY
    ( IF( a.is_main_favorite = "y", 1, 0 )
      +
      IF( b.is_main_favorite = "y", 1, 0 ) ) DESC

By the way: You may want to store 1/0 instead of y/n in the database so that you won't need the IF call

0

Try this SQL statement:

SELECT 
    s.state, u.company 
FROM 
    states s, users u 
WHERE 
    s.company_id = u.company_id 
AND 
    s.state = "$state" 
GROUP BY 
    s.state 
ORDER BY 
    s.state, u.company

This should give you an alphabetic list of companies per state selected. The ORDER BY uses the state first in case you decide to expand your query to get more than one states.

2
  • I tried this code and I do understand that it is better than what I had, but I am still struggling with it. After your statement, I wrote 'while($row=mysql_fetch_assoc($result)){echo $row['auction_company'];};' to echo the data that I need listed. It would only echo the first company name in that state. What do you think I doing wrong?
    – JayK
    Sep 6, 2012 at 21:54
  • I figured out the issue. I removed the 'GROUP BY' and it works great. Thank you very much.
    – JayK
    Sep 6, 2012 at 23:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.