Help with MySQL JOIN statement - php

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)

Related

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

SELECT data from 4 tables in MySQL

I have a problem with MySQL statement:
SELECT
oxarticles.OXTITLE AS TITLE,
oxmanufacturers.OXTITLE_1 AS OXMANTITLE,
oxarticles.OXDISTEAN AS OXDISTEAN,
oxarticles.OXMPN AS MPN,
oxarticles.OXPRICE AS OXPRICE,
oxarticles.OXSTOCK AS OXSTOCK,
oxarticles.OXARTNUM AS OXARTNUM,
oxseo.OXSEOURL AS OXSEOURL,
oxartextends.OXLONGDESC_1 AS OXLONGDESC
FROM `oxarticles`
INNER JOIN `oxartextends` ON oxarticles.OXID = oxartextends.OXID
INNER JOIN `oxmanufacturers` ON oxarticles.OXID = oxmanufacturers.OXID
INNER JOIN `oxseo` ON oxarticles.OXID = oxseo.OXOBJECTID;
My problem is that tables oxarticles and oxmanufacturers have two same column names OXID and OXTITLE_1 but the above code doesn't work. Please help.
You are trying to match article id to manufacturer id. That's wrong. You need to join your article table to the manufacturer table using the manufacturer id, not the article id.
In your case, that is oxarticles.OXMANUFACTURERID for the manufacturer id in the article table and oxmanufacturers.OXID in the manufacturer table.
SELECT
oxarticles.OXTITLE AS TITLE,
oxmanufacturers.OXTITLE_1 AS OXMANTITLE,
oxarticles.OXDISTEAN AS OXDISTEAN,
oxarticles.OXMPN AS MPN,
oxarticles.OXPRICE AS OXPRICE,
oxarticles.OXSTOCK AS OXSTOCK,
oxarticles.OXARTNUM AS OXARTNUM,
oxseo.OXSEOURL AS OXSEOURL,
oxartextends.OXLONGDESC_1 AS OXLONGDESC
FROM `oxarticles`
INNER JOIN `oxartextends`
ON oxarticles.OXID = oxartextends.OXID
INNER JOIN `oxmanufacturers`
ON oxarticles.OXMANUFACTURERID = oxmanufacturers.OXID
-- ^^^
-- Here's the manufacturer id
-- in the article table
INNER JOIN `oxseo`
ON oxarticles.OXID = oxseo.OXOBJECTID;

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

MySQL RIGHT JOIN SUM() from Table B to Info on Table A

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

SQL Join question

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.

Categories