SQL Join question - php

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.

Related

relations in SELECT query (PHP)

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';

How can i filter my data from mysql?

Let's say i have 3 tables:
table: SONGS
cell: ID primary key
cell: TITLE
cell: ADD_TIME
table: CATEGORIES
cell: ID primary key
cell: title
table: SONGS_CATEGORIES
cell: ID primary key
cell: SONG_ID
cell: CATEGORY_ID
Then let's assume that I have 3 songs(with id's 0, 1, 2), and I have 5 categories(rock, pop, rap and so on) and respectively, their id's(0, 1, 2...). How my sql query should look like, if I want to be able to select more than one category, then I need to look into table SONGS_CATEGORIES and get all songs_id with category id I selected, and then i get songs from table SONGS with those id's?
Hope i explained what i need pretty well, English is not my native language.
If you already know the ID numbers of the categories that you're wanting songs from then the following should do the trick:
SELECT *
FROM SONGS s
JOIN SONGS_CATEGORIES sc
ON s.ID = sc.SONG_ID
WHERE sc.CATEGORY_ID IN (#id1, #id2, #id3...#idN);
If you only know the names of the categories you want the songs for then you can use this:
SELECT *
FROM SONGS s
JOIN SONGS_CATEGORIES sc
ON s.ID = sc.SONG_ID
JOIN CATEGORIES c
ON sc.CATEGORY_ID = c.ID
WHERE c.title IN (#catTitle1, #catTitle2, #catTitle3...#catTitleN);
In both of these examples you would replace the # with the data you are providing and adjust the SELECT part to show only the columns you need.
If you want to select songs from categories 1 & 2 :
SELECT s.id song_id, s.title song_title, s.add_time song_add_time, c.id cat_id, c.title cat_title
FROM CATEGORIES c
INNER JOIN SONGS_CATEGORIES sg
ON c.id = sg.category_id
INNER JOIN SONGS s
ON sg.song_id = s.id
WHERE c.id = 1 OR c.id = 2
I guess this is what you need
SELECT * -- select what ever columns you need to display
FROM SONGS s
JOIN SONGS_CATEGORIES sc
ON s.ID = sc.SONG_ID
JOIN CATEGORIES c
ON c.ID = sc.CATEGORY_ID
where c.title in ('title1','title2')

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
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

PHP MySQL item categories

I'm trying to display a list of items with their name and all categories they belong to.
My database structure is:
Items table with Id, Item_Name.
Categories table with Id, Category_Name
Items_Categories table with Id, Item_id, Category_id.
I'd like to display the results in a table with the Item Name, and a comma-delimited list of its Categories' Names. I'm not sure how to do this with a single query. Thanks in advance.
You need to use the GROUP_CONCAT function that is available in mysql:
select i.item_name, group_concat(c.category_name) as categories
from items i
inner join items_categories ic on i.id = ic.item_id
inner join categories c on ic.category_id = c.id
group by i.item_name
I won't write it for you, but you need to use group_concat() function (MySQL).
I must say I did not test this query, but it should be something similar to the following:
SELECT i.Id, i.Item_name, GROUP_CONCAT(c.Category_Name) AS category_list
FROM Items i
JOIN Items_categories ci ON ci.Item_id = i.Id
LEFT JOIN Categories c ON c.Id = ci.Id
GROUP BY i.Id
Note that you can use GROUP_CONCAT to create a comma seperated list.
SELECT i.Item_Name, GROUP_CONCAT(c.Category_Name) AS Category_List
FROM Items AS i
LEFT OUTER JOIN (
Items_Categories AS ic
INNER JOIN Categories AS c ON ic.Category_id = c.Id
) ON i.Id = ic.Item_id
GROUP BY i.Id;

problem in php mysql

i want to get distinct category name from both photos & videos tables which have category name as in categories table...
here, m trying to execute a mysql query to get distinct categories name from main category table which are coming in both or even in one table i.e. photos & videos...
mysql query:
SELECT DISTINCT t1.category_name FROM categories t1, photos t2, videos t3 WHERE
t1.category_name=t2.category OR t1.category_name=t3.category AND t2.block=0 AND
t3.block=0 ORDER BY t1.category_name asc
here's the structure of all tables...
CATEGORIES:: (MAIN TABLE)
PHOTOS:: (CONTAINS PHOTOS OF ABOVE CATEGORIES)
VIDEOS:: (CONTAINS VIDEOS OF ABOVE CATEGORIES)
please, help me...
I'd use an EXISTS clause
SELECT c.category_name
FROM categories c
WHERE EXISTS (
SELECT 1 FROM photos p
WHERE p.category = c.category_name
UNION
SELECT 1 FROM videos v
WHERE v.category = c.category_name
)
If i understand the question right this would work:
select distinct categories.name
from categories
left join photos on photos.category = categories.name
left join videos on videos.category = categories.name
where (photos.id IS NOT NULL) OR (videos.id IS NOT NULL);
But you should use a foreign key for your connection between category and photo/video.
http://dev.mysql.com/doc/refman/5.1/de/innodb-foreign-key-constraints.html
Change the datatype of photos.category to (INT)
and the videos.category to (INT) it will be way faster when the table become larger
SELECT t1.category_name
FROM categories t1,
JOIN photos p ON p.category = t1.id
JOIN videos v ON v.category = t1.id
GROUP BY t1.category_name
ORDER BY t1.category_name ASC
You need to change the datatype of photos.category to INT instead of varchar and do the same for videos.category, then try below query.
SELECT Cat.category_name
FROM categories As Cat,
JOIN photos AS P ON P.category = cat.id JOIN videos AS vid ON vid.category = Cat.id
GROUP BY Cat.category_name
ORDER BY Cat.category_name ASC
thanks.

Categories