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
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";
SELECT c.id as cat_name from category as c , items as i where c.id=i.cat_id
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.
SELECT
category.cat_name
FROM
category
JOIN
items ON category.cat_id = items.field4
Related
I have little problem with SQL query in PHP.
I want to get from table post rows with category ex. 'art'.
This table don't contain name of my category (only category_id).
So, how to connect this in my query?
Tables:
post:
id, title, category_id
category:
id, name
I tried this way, but it not works.
SELECT * FROM post WHERE category_id = category.id AND category.name="art";
Anyone can help me? Thanks.
You need to join the tables.
Given your schema, this should give you an starting point:
SELECT p.* FROM post p INNER JOIN category c ON p.category_id = c.id WHERE c.name = 'art';
You are missing the JOIN
SELECT * FROM post
INNER JOIN category
WHERE category_id = category.id AND category.name="art";
You need to use join to query on name column of category table, e.g.:
SELECT p.*
FROM post p JOIN category c ON p.category_id = c.id
WHERE c.name = 'art';
I have normalized tables I want to select the items that belong to the userid
I'm familiar with select syntax but I'm very weak in joins tables so I'm a bit confused on how to get the items that belong to the user should I use join ? or is there other way
this is just simple example of my tables they have more fields
..........
user
..........
userid
firstname
address
..........
items
..........
itemsid
itemName
itemDescription
..........
user_items
..........
userid(FK)
itemsid(FK)
Use two inner join
select a.*, b.*
from user_items as c
inner join user as a on a.userid = c.userid
inner join items as b on b.itemsid = c.itemsid;
Use INNER JOIN
SQL
select user.*, items.*
from user_items
inner join user on user.userid = items.userid
inner join items on items.itemsid = user_items.itemsid;
So if I read this correctly, user_items.userid = user.userid.
So you want to join, something like this.
SELECT i.itemsid, i.itemName, i.Description FROM items i JOIN users us ON ui.userid = us.userid JOIN user_items ui ON ui.itemsid = i.itemsid WHERE ui.userid = VALUE;
Replace Value with your actuall user id
Basically I have a load of product information on Table A. This include the product_id which is the common id over both tables. On Table B I have a list of votes which include product_id, username, thevote(could be +1 or -1).
So basically I want to have a table of 'Table A' with a additional column containing the SUM of all the votes for that product_ID. I am sure there is an easy way to do this. Which i think is using 'right join'.
I still want it to list all the products in Table A regardless if they have a single +1 or -1 vote.
Many thanks in advance peoples!
You could use group by statement and left join like:
select productName, sum(vote) as productVoteSum
from `products` p
left join `products_votes` pv on p.id = pv.productId
where productName like '%chocolat%'
group by p.id
order by productName;
You can do a LEFT JOIN (that goes through all the information on tableA and puts the SUM in the ones that have the matching product_id on tableB), then you add the SUM(b.thevote) and group by the remaining columns
SELECT a.product_id,a.productName,SUM(b.thevote)
FROM tableA a
LEFT JOIN tableB b ON a.product_id = b.product_id
GROUP BY a.product_id, a.productName
SELECT a.*, SUM(b.votes) FROM TableA a LEFT JOIN TableB b ON a.prouct_id=b.product_id
GROUP BY a.product_id
I have a table vm_category in which I fetch all rows from the category_id and caregory_name columns. I then use this in a drop-down to populate a search-filter module on the left side of the side.
Now doing so gives me a gigantic list of categories, and I'd like to narrow it down by category's sub-category. Those are organized in another table called vm_category_xref in which I have 3 columns;
category_parent_id
category_child_id
category_list
What I'd like to do is specify in my query to only return the category_id and names that match a specific category_parent_id in the _xref table. Here is my current query:
$query = "SELECT `c`.`category_name` AS `name` , `c`.`category_id` AS `id`
FROM `#__vm_category` AS `c` ORDER BY `c`.`category_id`;";
I am not familiar with JOIN enough to figure this out, but I know it should be rather simple, and any help would be much appreciated. Thank you!
Try this:
SELECT
c.category_name AS name,
c.category_id AS id
FROM #__vm_category AS c
INNER JOIN vm_category_xref xr
ON xr.category_child_id = c.category_id
WHERE xr.category_parent_id = 5 /* the parent_id you want to filter on */
ORDER BY c.category_id;
Try this:
SELECT c.category_name as name, c.category_id as id
FROM vm_category as c
INNER JOIN vm_category_xref as x ON
x.category_parent_id = c.category_id
ORDER BY c.category_id
Essentially this returns category_name and category_id only for records that have a category_id that equals a category_parent_id in _xref.
SELECT c.category_name AS name , c.category_id AS id
FROM vm_category c
INNER JOIN vm_category_xref x on c.category_id = x.category_child_id
ORDER BY c.category_id
I may have picked the wrong fields to join on but whichever are the 2 same fields in c and x are the ones you should put in the join condition
select vc.category_id, vc.category_name
from vm_category vc, vm_category_xref vcx
where vc.category_id = vc.category_parent_id
SELECT
c.category_name AS name
,c.category_id AS id
FROM
vm_category AS c
join vm_category_xref vcx
on c.category_id = vcx.category_parent_id
where vcx.category_parent_id = ?
ORDER BY c.category_id;
Replace ? with the id you want to match.
If I'm understanding you correctly, you want to retrieve category_name and category_ID only for certain category parent IDs on the xref.
I typically use IN for situations like these.
I'm assuming that category_id in vm_category maps to category_parent_id in vm_category_xref.
So...
SELECT CATEGORY_NAME, CATEGORY_ID
FROM VM_CATEGORY
WHERE CATEGORY_ID IN
(SELECT CATEGORY_PARENT_ID FROM VM_CATEGORY_XREF
WHERE CATEGORY_PARENT_ID IN ('XXX', 'YYY', 'ZZZ'))
XXX, YYY, ZZZ are the IDs for the categories you want.
I have 2 tables:
Table A is a category table. Columns are cid, catname.
Table B is a relationship table. Columns are cid, parent (parent is another cid).
Here's where I am so far:
"SELECT c.cid, c.catname AS catname, r.parent AS parent FROM tableA AS c JOIN tableB AS r ON r.cid=c.cid";
I know I'll get 3 columns (2 from tableA and one from tableB) but I also want to get the catname value from the parent in tableA If I were to do a second query, it would look like this (assuming we put the result into a $row variable):
"SELECT catname FROM tableA WHERE cid='".$row['parent']."'";
That way I can display it as text.
What do I add, and where? Is there a second JOIN?
You can join a table multiple times:
SELECT a.cid AS acid, a.catname AS aname,
b.cid AS bcid, b.catname AS bname
FROM relationships AS r
JOIN categories AS a ON (r.cid = a.cid)
JOIN categories AS b ON (r.parent = b.cid)