I Have 2 tables:
Table 1 named category_desription, it contains 2 fields: category_name and category_id.
Table 2 named category includes the category_id and parent_Category_id fields which are foreign keys from category_id in the first table.
I want to select 4 fields whereby the result will contain category_id and its name category_name and parent_category_id and its name as well which happens to be category_name as well
I couldn't figure out how to do it so far so I wish if someone assist me in this.
I hope this will solve your prblm
SELECT category_id,
(Select category_name from table1 where category_id= table2.category_id) as category_name ,
parent_category_id,
(Select category_name from table1 where category_id= table2.parent_category_id)
as parent_category_name from table2
with category_desc as
(select 1 as category_id, 'Category 1' as category_name
from dual
union all
select 2 as category_id, 'Category 2' as category_name from dual),
category as
(select 1 as category_id, 2 as parent_category_id from dual)
select c.category_id,
c_d.category_name,
c.parent_category_id,
pc_d.category_name parent_category_name
from category c
join category_desc c_d
on c_d.category_id = c.category_id
join category_desc pc_d
on pc_d.category_id = c.parent_category_id
use category_desc with different alias to find parent_category details
This is a simple name conflict issue in SQL, which can be solved easily by using alias
e.g.
Select t1.category_id, t1.category_name, t2.category_id, t2.category_name
from table1 t1 join table2 t2 on <some condition..>
You can also give different names to the result column
Select t1.category_id, t1.category_name as 'Parent Category Name',
t2.category_id, t2.category_name as 'Category Name'
from table1 t1 join table2 t2 on <some condition..>
I hope it solves your issue.
Related
I have a database table that looks like that:
I want to pull all product_id from such table that have certain category_id set, AND this record is the only one for that product ID. (if there are two records for product_id with any category_id, it shouldn't be included).
I tried something like that:
SELECT product_id
FROM `products_categories`
WHERE category_id = 541
AND HAVING COUNT(product_id) = 1;
But that returns a syntax error that I can't figure out. Anyone can advice what I may be doing wrong?
SELECT product_id
FROM products_categories t1
WHERE category_id = 541
AND NOT EXISTS ( SELECT NULL
FROM products_categories t2
WHERE t1.product_id = t2.product_id
AND t1.category_id <> t2.category_id );
Here's the query i'm using
SELECT
t1.name AS product_name,
t1.description AS product_description,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 1) AS product_category,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 2) AS product_category2,
(SELECT t2.name from table2 order by name ASC LIMIT 1
OFFSET 3) AS product_category3
FROM table1 t1 INNER JOIN
table3 t3
ON t3.product_id = t1.product_id INNER JOIN
table2 t2
ON t2.category_id = t3.category_id
;
The tables i'm using have the following information:
table1: product_id, name, description
table2: category_id, name, description
table3: product_id, category_id
and i get this result
name - description - category
NAME 1 - Description 1 - Category 1
NAME 2 - Description 2 - Category 1
What i'm aiming for is the following result:
name - description - product_category -category2 - category3
NAME 1 - Description 1 - Cat1 - Cat2 - Cat5
NAME 2 - Description 2 - Cat7 - Cat9 - Cat11
I would have preferred to see some actual data, but one thing about your query that stands out is that you are including the product_id in your GROUP BY clause. I don't think it belongs there, and you should not need it. Instead, just group by the name and description.
SELECT
t1.name AS product_name,
t1.description AS product_description,
GROUP_CONCAT(t2.name) AS product_category
FROM table1 t1
INNER JOIN table3 t3
ON t3.product_id = t1.product_id
INNER JOIN table2 t2
ON t2.category_id = t3.category_id
GROUP BY
t1.name,
t1.description;
One explanation for why you were originally getting a single row is that the product_id serves as something like a primary key in table1. In this case, you were effectively telling MySQL to group around individual records. This would mean that GROUP_CONCAT() would only act on single records, leading to your current output.
Let's say I have a Table that looks like this:
id fk value
------------
1 1 'lorem'
2 1 'ipsum'
3 1 'dolor'
4 2 'sit'
5 2 'amet'
6 3 'consetetur'
7 3 'sadipscing'
Each fk can appear multiple times, and for each fk I want to select the last row (or more precise the row with the respectively highest id) – like this:
id fk value
------------
3 1 'dolor'
5 2 'amet'
7 3 'sadipscing'
I thought I could use the keyword DISTINCT here like this:
SELECT DISTINCT id, fk, value
FROM table
but I am not sure on which row DISTINCT will return and it must be the last one.
Is there anything like (pseudo)
SELECT id, fk, value
FROM table
WHERE MAX(id)
FOREACH DISTINCT(fk)
I hope I am making any sense here :)
thank you for your time
SELECT *
FROM table
WHERE id IN (SELECT MAX(id) FROM table GROUP BY fk)
Try this:
SELECT a.id, a.fk, a.value
FROM tableA a
INNER JOIN (SELECT MAX(a.id) id, a.fk FROM tableA a GROUP BY a.fk
) AS b ON a.fk = b.fk AND a.id = b.id;
OR
SELECT a.id, a.fk, a.value
FROM (SELECT a.id, a.fk, a.value FROM tableA a ORDER BY a.fk, a.id DESC) AS a
GROUP BY a.fk;
Try this:
SELECT t.* FROM
table1 t
JOIN (
SELECT MAX(id) as id
FROM table1
GROUP BY fk
) t1
ON t.id = t1.id
Inner query will give you highest id for each fk using MAX(). Then we join this inner table with your main table.
You could also do
SELECT id, fk, value FROM table GROUP BY fk HAVING id = MAX(id)
I don't have mysql here, but it works in Sybase ASE
What I am trying to do:
I want to get the name of the product with it's id from orders table , but in my case I have stored products in 3 different tables.
i.e dish,drinks,others.
Problem:
So the problem is in orders table i have put a type(what kind of product it is) like "dirnks,dish,others".So how can i get the name of the product when product can be in any tables(dish,drinks,others)
What i want:
I don't whether it's possible or not but if anyone can tell what below query is there,how can i use select statement in IF statment
SELECT product_id,type,quantity ,
( IF(type='Drinks)
THEN
(SELECT drink_name FROM drinks WHERE drink_id=product_id)
ELSE IF (type='Dish)
THEN
(SELECT dish_name FROM dish WHERE dish_id=product_id)
)
as drink_name
FROM order_product
WHERE shop_id='JSMMSK730'
Is this thing possible or not?
Use a UNION, and add a new column to the subquery that indicates which table the rows came from. Then you can use this in the JOIN condition.
SELECT product_id, o.type, quantity, name FROM
order_product o JOIN
(
SELECT "dish" type, dish_id id, drink_name name from dish
UNION
SELECT "drinks" type, drink_id id, drink_name name from drinks
UNION
SELECT "others" type, other_id id, other_name name from others
) p
ON p.id = o.product_id and p.type = o.type
WHERE shop_id = 'JSMMSK730'
Try below Syntax:
SELECT * FROM
(
SELECT product_id, product_name from dish
UNION
SELECT product_id, product_name from drinks
UNION
SELECT product_id, product_name from others) as product, order
)
WHERE product.product_id = order.order_id
I have these two tables:
Table Author:
ID (INTEGER),
author_name (VARCHAR),
first_name (VARCHAR),
last_name (VARCHAR),
preferred_name (VARCHAR)
Table CoAuthored:
ID (INTEGER)
author1ID (INTEGER),
author2ID (INTEGER),
paper_ID (INTEGER) // (ID of the co-authored paper referenced by this link)
As you can see, both tables have a column 'ID', but they are unrelated.
However, when I use this query:
select author_name, count(*) as NumPapers from
(select * from Author as a join CoAuthored as c on a.ID = c.author1ID
union distinct
select * from Author as b join CoAuthored as d on b.ID = d.author2ID) as t1
group by author_name
order by NumPapers;
mySQL gves me an error saying: ERROR 1060 (42S21): Duplicate column name 'ID'
Why does this happen and how do I avoid it?
Rather than select * ... use select author_name ... in the two subqueries being unioned. The problem stems from both Author and CoAuthored having ID columns. SELECT * brings through both of these columns and MySQL doesn't like the idea of UNIONing these to produce a resultset with two same-named columns.
Try this:
select author_name, count(*) as NumPapers from
(
select a.id, a.author_name from Author as a
join CoAuthored as c on a.ID = c.author1ID
union distinct
select b.id, b.author_name from Author as b
join CoAuthored as d on b.ID = d.author2ID
) as t1
group by author_name
order by NumPapers;
Since you do not need the ID column from CoAuthored, you can just not select it in the inner query. This should remove your duplicate column error since it is only selecting 1 of the ID columns.
what about:
SELECT author_name, COUNT(*) AS NumPapers
FROM Author AS a
JOIN CoAuthored AS c ON a.ID = c.author1ID OR a.ID = c.author2ID
GROUP BY author_name
ORDER BY NumPapers;