Jump to content

Recommended Posts

I have 2 tables

 

Fruits

idnamecolor

1applies1,2

2oranges3

 

Colors

idname

1red

2green

3orange

 

Now I have this query "SELECT *.fruit, colors.name AS color FROM fruits, colors".

 

Now how can I deal with the results in the color record of apples as apples can be red or green so I can have the array be something like.

 

idnamecolor

1applesred, green

2orangesorange

 

If this is more a PHP question I apologize!

Link to comment
https://forums.phpfreaks.com/topic/140037-help-with-query/
Share on other sites

You need to normalize the tables to do this most efficiently.  (Look into normalized SQL schema.)

 

 

Anyway, I would do it something like this:

 

 

CREATE TABLE fruits (

    fruit_id INT NOT NULL AUTO_INCREMENT PRIMARY_KEY,

    fruit_name varchar(32)

);

 

CREATE TABLE colors (

    color_id INT NOT NULL AUTO_INCREMENT PRIMARY_KEY,

    color_name varchar(32)

);

 

CREATE TABLE fruit_colors (

    fruit_id INT NOT NULL,

    color_id INT NOT NULL

);

 

 

INSERT INTO fruits (name) VALUES ('Apples'), ('Oranges');

 

INSERT INTO colors (name) VALUES ('Red'), ('Green'), ('Orange');

 

INSERT INTO fruit_colors (color_id, fruit_id) VALUES (1, 1);

INSERT INTO fruit_colors (color_id, fruit_id) VALUES (2, 1);

INSERT INTO fruit_colors (color_id, fruit_id) VALUES (3, 2);

 

 

 

 

 

Then, to select all fruits with their colors:

 

 

 

SELECT f.fruit_id, f.name, c.name FROM fruits f INNER JOIN fruit_colors fc ON fc.fruit_id = f.fruit_id INNER JOIN colors c ON c.color_id = fc.color_id;

 

 

Yeah, a beast of a query, but it would keep you from having to pull all of the data into PHP and do more queries.  It would also help prevent repetitive data.  In this case, this could be seen by some as over normalizing though, since you will probably have a relatively small amount of rows, and colors wouldn't take much space.  (There are advantages to the middle table though.  Less repetition, thus smaller size, and easier/more uniform changes, for the most part.)

 

 

Some problems with the schema I did above:

 

-You would want indexes on some fields.

-You might not want to use a 32 bit integer if you plan on using fruits....  A short would probably be fine.  32k fruit (or 65k unsigned) is a lot of fruit.  (Even a small int might be ok with 255 numbers when unsigned, but you never know.)

Link to comment
https://forums.phpfreaks.com/topic/140037-help-with-query/#findComment-732904
Share on other sites

I tried to solve this earlier using the exact same structures and display it CSV style(CONCAT_WS) like he asked in the OP.  I used similar, but different naming conventions however.

My "equivalent" query was:

SELECT f.fruit_id, f.name, CONCAT_WS(',',c.name) as colors FROM fruits f INNER JOIN Fruit_Colors fc ON fc.fruit_id = f.fruit_id INNER JOIN colors c ON c.color_id = fc.color_id
GROUP BY f.fruit_id

 

Which doesn't give me a csv list as expected... (for row 1)

Link to comment
https://forums.phpfreaks.com/topic/140037-help-with-query/#findComment-733024
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.