hello all i have to join two queries to form an result set :-
1st:
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId
FROM
ecart_product
INNER JOIN ecart_orderdetail
WHERE
ecart_orderdetail.productId = ecart_product.id
group by orderId
this gives me
orderId catId
167 59, 2
168 2
169 2
170 2
171 2
172 48, 2
173 2
174 2
2nd:
select * from ecart_orders
in both orderId is common how could this could be join ?
Assuming that orderId is PK in ecart_orders table and that it has productId column.
You can try this:
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId ,
ecart_orders.*
FROM
ecart_product
INNER JOIN ecart_orderdetail
ON ecart_orderdetail.productId = ecart_product.id
INNER JOIN ecart_orders
ON ecart_orders.productId = ecart_product.id
group by orderId
In mean time i also solved it
select * from ecart_orders Inner join(
SELECT
orderId,
GROUP_CONCAT(DISTINCT(categoryId) ORDER BY orderId SEPARATOR ', ') as catId
FROM `ecart_product` INNER JOIN ecart_orderdetail
WHERE ecart_orderdetail.productId = ecart_product.id group by orderId) as c
on ecart_orders.id=c.orderId
Related
I got a table like:
product
id | product_id
1 55
2 56
3 57
product_options
id | product_id | options_value
1 55 88
2 55 87
3 55 89
... ...
I want to select all option_values from product_options where product_id from product is same with product_id from product_options.
After I select all fields from product I use this:
$sql .= " LEFT JOIN " . DB_PREFIX . "product_option_value ovi ON (ovi.product_id=p.product_id)";
if(...){
$sql .= " AND ovi.option_value_id='$value'";
}
The problem is: If I only got one options_value, it's fine.
but when I have 2 or more options_values the result is 0.
i want to select all options_value from product_options for all product_id from product_options
PS. Sorry for my english and explication
Use right join with product_id
select p.id, p.product_id, po.options_value from products p right join product_options po on p.product_id=po.product_id
Use inner join between two table using product_id as join key
select p.id, p.product_id, po.optional_value
from products p inner join product_options po on p.product_id=po.product_id
I want to select the rows from products table.
The products are season based.
each product row/entity contains a column named id_season
and seasons table looks like
id | season_name | active | created | modified
Season names are Year like 2016,2017,2018 ...
I want to select all the products from 2016 and 2017 which have same code
I have a simple select like
SELECT *
FROM products P
INNER JOIN seasons S ON S.id = P.id_season
WHERE S.active = 1
AND S.season_name IN ( YEAR(GETDATE()), YEAR(GETDATE()) + 1 )
but don't know how to refine it, to match the codes on products from different seasons.
Try with the below code..
;WITH cte_1
AS
(SELECT *,COUNT(p.code) OVER(partition by p.code Order by p.code) cnt
FROM products P
INNER JOIN seasons S ON S.id = P.id_season
WHERE S.active = 1
AND S.season_name IN ( YEAR(GETDATE()), YEAR(GETDATE()) + 1 )) -- or simply put IN ('2016','2017')
SELECT *
FROM cte_1
WHERE cnt>1
or you can use a subquery format as below.
SELECT *
FROM
(SELECT *,COUNT(p.code) OVER(partition by p.code Order by p.code) cnt
FROM products P
INNER JOIN seasons S ON S.id = P.id_season
WHERE S.active = 1
AND S.season_name IN ( YEAR(GETDATE()), YEAR(GETDATE()) + 1 )) t
WHERE t.nt>1
You can use inner query as follows:
SELECT * FROM products
where id_season in (Select id from seasons where
season_name IN ( YEAR(GETDATE()), YEAR(GETDATE()) + 1 ));
I am having an issue where I want to join several columns by an id.
My first table looks like this:
submitter_id reviewer_id processor_id
75 34 91
The table that I want to join looks like this:
id first_name last_name
75 Bob Smith
34 Albert McDonald
91 Joe Blo
I am trying to create a query that will look at each id in my first table and then get the first and last name's for each id.
For example, a query that does this should return something like:
[
75 => "Bob Smith",
34 => "Albert McDonald",
91 => "Joe Blo"
];
Can anybody help me construct a query that can accomplish this? Thanks!
Join the same table 3 times with different alias names
select t1.submitter_id, t1.reviewer_id, t1.processor_id,
t2.first_name as submitter_firstname, t2.last_name as submitter_lastname,
t3.first_name as reviewer_firstname, t3.last_name as reviewer_lastname,
t4.first_name as processor_firstname, t4.last_name as processor_lastname
from firstTable t1
left join namesTable t2 on t1.submitter_id = t2.id
left join namesTable t3 on t1.reviewer_id = t3.id
left join namesTable t4 on t1.processor_id = t4.id
I think what your actually looking for is more like this:
SELECT n.id, CONCAT(n.first_name, ' ', n.last_name)
FROM names n
JOIN ids i
ON n.id = i.submitter_id
OR n.id = i.reviewer_id
OR n.id = i.processor_id
GROUP BY n.id;
This is only doing one join, shows the records with the 2 columns you actually want and restricts so users are only listed 1 time. Also since you probably don't want to return just the ID if the user doesn't have a name setup you don't want a left join.
Edit:
If you need indexes you can make one on submitter_id, reviewer_id, processor_id if the performance is needed for you.
You want all in one resultset, so you could use UNION ALL:
SELECT p.id, CONCAT(p.first_name, ' ', p.last_name) as name
FROM person p
JOIN firstTable f
ON p.id = f.submitter_id
UNION ALL
SELECT p.id, CONCAT(p.first_name, ' ', p.last_name) as name
FROM person p
JOIN firstTable f
ON p.id = f.reviewer_id
UNION ALL
SELECT p.id, CONCAT(p.first_name, ' ', p.last_name) as name
FROM person p
JOIN firstTable f
ON p.id = f.processor_id
I think this is a better approach if you plan to use some conditions only for some group, like where reviewer_id > 100. If not, the Sir. Egole is cleaner.
Assuming that you need one row in the first table to return one row at the result.
SELECT tt.submitter_id , CONCAT(ta.first_name, ' ', ta.last_name) ,
tt.reviewer_id , CONCAT(tb.first_name, ' ', tb.last_name) ,
tt.processor_id , CONCAT(tc.first_name, ' ', tc.last_name)
FROM `transaction` as tt , `names` as ta , `names` as tb , `names` as tc
WHERE tt.submitter_id = ta.id AND
tt.reviewer_id = tb.id AND
tt.processor_id = tc.id
The result would be the 3 ids with the corresponding names in one row per transaction.
I have a table:
The select db is:
$select_table2 = '
SELECT e.product_clicks
, e.product_id
, e.website_url
, u.name
, u.product_id
FROM `oc_aa_affiliatecollclicktracking` AS e
LEFT
JOIN `'.DB_PREFIX.'product_description` AS u
ON e.product_id = u.product_id
GROUP
BY e.website_url';
This will group it but won't list all product_id
I get:
http://127.0.01
36
I'd like to group it but see all the product_id
http://127.0.01
36
40
33
$select_table2 = 'SELECT `product_id` FROM `oc_aa_affiliatecollclicktracking` WHERE website_url LIKE '%http%';
if you dont want to repeat product_id
$select_table2 = 'SELECT DISTINCT(`product_id`) FROM `oc_aa_affiliatecollclicktracking` WHERE website_url LIKE '%http%';
My table Structure is like below
vendor_id account_id price code
27 2 0.058 91
29 2 0.065 91
23 2 0.043 91
30 2 0.085 91
31 3 0.085 91
I have to get the the minimum price where code should be equal to 91, price between given range and group by account_id
I am using the select query as
select MIN(price) as min_price, account_id, vendor_id from tbl_input_values where code='91' and price>=0 and price<=2 group by account_id
And i am getting the output as
min_price account_id vendor_id
0.043 2 27
0.085 3 31
But it should be
min_price account_id vendor_id
0.043 2 23
0.085 3 31
Try this query -
SELECT
t1.*
FROM tbl_input_values t1
JOIN (
SELECT
MIN(price) AS min_price,
account_id
FROM tbl_input_values
WHERE code = '91' AND price >= 0 AND price <= 2
GROUP BY account_id
) t2
ON t1.account_id = t2.account_id AND t1.price = t2.min_price
Your query is correctly selecting the minimum price per account_id, but it's necessary to correlate this price with the vendor_id.
Here's my version :
SELECT iv.price AS min_price, iv.account_id, iv.vendor_id
FROM tbl_input_values AS iv
INNER JOIN (select min(price) AS min_price, account_id
FROM tbl_input_values
WHERE full_code='91' AND price>=0 AND price<=2
GROUP BY account_id) AS mp
ON iv.price=mp.min_price AND iv.account_id=mp.account_id;
If two vendors have the same minimum price for an account_id, this query returns both.
See the SQL fiddle here http://sqlfiddle.com/#!2/96d24/8
Try this query
SELECT
price,
account_id,
vendor_id
from
Table1
where
(vendor_id, price) in
(select
vendor_id,
min(price)
from
Table1
WHERE
code = '91' AND
price >= 0 AND
price <= 2
group by
account_id)
Without subquery using join
SELECT
a.price,
a.account_id,
a.vendor_id
from
Table1 a,
(select
vendor_id,
min(price) as 'price'
from
Table1
where
code='91' and
price>=0 and
price<=2
group by
account_id) b
WHERE
a.vendor_id = b.vendor_id AND
a.price = b.price
try;
SELECT tData.price, tData.account_id, tData.vendor_id FROM tbl_input_values, (SELECT price, account_id, vendor_id FROM tbl_input_values ORDER BY price ASC) AS tData WHERE tbl_input_values.account_id = tData.account_id GROUP by tbl_input_values.account_id;