Reputation: 37
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
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
Reputation: 10131
SELECT c.id as cat_name from category as c , items as i where c.id=i.cat_id
Upvotes: 1
Reputation: 4888
SELECT
category.cat_name
FROM
category
JOIN
items ON category.cat_id = items.field4
Upvotes: 0