Biplob Hossain
Biplob Hossain

Reputation: 37

MySQL query from two tables with multiple joining

I've two tables, example - category & items.

 1. category table has two fields- cat_id, cat_name.
 2. items table has 4 fields field1, field2, field3, field4 which value
    is cat_id.

Now, I need to print cat_name instead of cat_id in each row.

How can I do that? What will be the MySQL query?

For more info please, have a look here- Url: http://smartmux.com/demo_roundflat/admin/ username: test password: test

Upvotes: 0

Views: 51

Answers (4)

dkasipovic
dkasipovic

Reputation: 6130

If I understood right you can do something like this:

SELECT items.*, c1.cat_name, c2.cat_name, c3.cat_name, c4.cat_name
FROM items
LEFT JOIN category AS c1 ON c1.cat_id = items.field1
LEFT JOIN category AS c2 ON c2.cat_id = items.field1
LEFT JOIN category AS c3 ON c3.cat_id = items.field1
LEFT JOIN category AS c4 ON c4.cat_id = items.field1;

Let me know if this is what you needed.

Upvotes: 0

SagarPPanchal
SagarPPanchal

Reputation: 10131

SELECT c.id as cat_name  from category as c , items as i where c.id=i.cat_id

Upvotes: 1

Abdul Manaf
Abdul Manaf

Reputation: 4888

SELECT 
    category.cat_name
FROM
    category
        JOIN
    items ON category.cat_id = items.field4

Upvotes: 0

Harshal
Harshal

Reputation: 3622

You need to use Join and your query should be like this :

"select t1.*, t2.cat_name FROM table1 as t1 JOIN table2 as t2 on t1.cat_id=t2.cat_id";

Upvotes: 1

Related Questions