I have two tables: product and url_alias
product table has two columns: product_id and model
url_alias table has two columns: product_id and keyword
So, product_id is unique & primary key for both tables.
Now, I want to add (append) info of product.model to url_alias.keyword if product_id of both tables is same.
For example:
product.product_id = 123
product.model = 987
product.product_id = 123
url_alias.keyword = my-first-book.html
Desired result = 987-my-first-book.html
How can I do it?
PS: '-' hyphen is also required to add with model.
Try CONCAT::
Select
product.product_id,
CONCAT(product.model,'-', url_alias.keyword)
from
product
inner join url_alias on (url_alias.product_id=product.product_id)
SELECT CONCAT(product.model, '-', url_alias.keyword) AS url
FROM product
INNER JOIN url_alias
ON product.product_id = url_alias.product_id
WHERE product.product_id = '123'
SELECT CONCAT(p.product_id,' - ',u.keyword ) AS res
FROM product AS p
LEFT JOIN
url_alias AS u USING (product_id)
WHERE p.product_id = 123
Note : Remove the WHERE condition if you want to get result for all the products
Related
For those familiar with Magento, I'm attempting to write a query to create new upsell entries in the catalog_product_link table, but with some unique criteria. My knowledge of MySQL (or any SQL) is basic at best.
I have two types of products in a table, which I can uniquely identify by joining a few other tables. I can get a unique list of the two types of product results like so:
SELECT cpev.entity_id AS product_id, cpev.value AS name
FROM catalog_product_entity_varchar AS cpev
INNER JOIN catalog_product_entity AS cpe
ON cpe.entity_id = cpev.entity_id
INNER JOIN eav_attribute AS ea
ON ea.attribute_id = cpev.attribute_id
INNER JOIN catalog_category_product AS cat
ON cat.product_id = cpev.entity_id
WHERE cat.category_id IN ( 41 )
AND ea.attribute_code = 'name'
GROUP BY product_id
ORDER BY product_id;
...where the two lists of results are based on changing WHERE cat.category_id IN ( 41 ).
What I'm attempting to do is match a single row from the first list of results to one or more rows from the second list based on the name. The full name from the first list will match the first portion of the name from the second list plus some text; for example:
list1, name: Afinia L801
list2, name: Afinia L801 On-site Maintenance
Contract
For each match, I need to insert a new row in a different table, like so:
INSERT INTO catalog_product_link (product_id, linked_product_id, link_type_id)
SELECT #idFromList1 AS product_id, #idFromList2 as linked_product_id, 4 as link_type_id
...
I'm not sure if this is something that can be accomplished with only SQL, or if this is a case where I need to write (for example) a PHP script to pull the two lists from the db, match them up, and then insert the results. But I'm still learning SQL so I'm trying to get a better grasp of what I can do with just a MySQL query.
You can select from the same table twice.
SELECT cpev.entity_id AS product_id, cpev.value AS name
FROM catalog_product_entity_varchar AS cpev
INNER JOIN catalog_product_entity AS cpe
ON cpe.entity_id = cpev.entity_id
INNER JOIN eav_attribute AS ea
ON ea.attribute_id = cpev.attribute_id
INNER JOIN catalog_category_product AS cat
ON cat.product_id = cpev.entity_id
INNER JOIN catalog_category_product AS cat2
ON cat2.product_id = cpev.entity_id
WHERE cat.category_id IN ( 41 )
AND cat2.category_id IN ( 42 )
AND ea.attribute_code = 'name'
GROUP BY product_id
ORDER BY product_id;
I have two table. First is product in which Admin will add the Products and another is product_detail in which vendors will add the detail. Multiple vendors can add same product with the product detail like selling price, offer price etc.
"I want to select products from products table and want to select product detail from product_detail table but only one row of product detail which has less selling price".
So i want to use CI active records to get the final output as product list with its detail.
Take this for example :
Create table #Product(id int, name varchar(100));
Create table #Product_detail(id int, product_id int, detail varchar(100));
insert into #Product values
(1,'Item1'),
(2,'Item2')
insert into #Product_detail values
(1,1,20),
(2,1,10),
(3,1,40),
(4,2,70),
(5,2,50)
this is your basic query:
select
a.*, b.price
from
#product a inner join
(select product_id, min(price) price
from #product_detail
group by product_id
) b
on a.id = b.product_id
In CI, try to do something like this:
$var = $this->db->query("select a.*, b.price from #product a
inner join
(select product_id, min(price) price
from #product_detail
group by product_id
) b
on a.id = b.product_id")->result_array();
sample output :
id name price
----------- ------ -----------
1 Item1 10
2 Item2 50
You first should make getters and setters in your model and using CI active record + pdo It'as more safe from my point of view.
Best regards.
I have 3 tables in my database, Categories,Subcategories and Articles.
My Articles table contains columns caled CategoryID and SubcategoryID, which are foreign keys of the table Categories and Subcategories.
My question is, how can I get the name of the Category and Subcategory stored only as ID's in my Articles Table.
I think I need a kind of subquery or join.
Here is what I have:
SELECT ArticleTitle
,CategoryID
,SubcategoryID
FROM Articles
WHERE SubcategoryID =
(SELECT SubcategoryName
FROM Subcategories
WHERE SubcategoryName = 'info'
)
When I execute this code in mysql, there are no erros, but I receive 0 results. All tables are containing some data.
change this:
where SubcategoryID = (select SubcategoryName from Subcategories
to this
where SubcategoryID in (select SubcategoryID from Subcategories
the changes were
the equal sign is now the word in.
the subquery is selecting SubcategoryID instead of SubCategoryName
Using Joins :
SELECT a.ArticleTitle AS ArticleTitle,
c.CategoryName AS CategoryName,s.SubcategoryName AS SubcategoryName
FROM
Articles a JOIN Categories c on a.CategoryID=c.ID JOIN Subcategories s on a.SubcategoryID=s.ID
WHERE s.SubcategoryName = 'info';
I'm trying to extend a 2 table join query to join 3 tables. I'm using PDO
SELECT tb.product, COUNT(tb.ID) AS nums FROM items tb LEFT JOIN itemCode cod ON tb.ID = cod.codeID WHERE tb.product = :product AND cod.name = :cName
The above 2 table join works. My question is, how do I match itemCode's availabilityID column to tblC's stockID column and add it to the WHERE clause with same query to get the result?
So basically I want to check if the availabilityID column of itemCode table match with stockID column of tblC table.
EDIT: table structure:
**items table**
ID | product
**itemCode table**
codeID | name
**tblC table**
stockID
As stated in your question you want to match availabilityID with stockID, this is what the query does. However, the availabilityID does not show up in your edit.
SELECT tb.product, COUNT(tb.ID) AS nums
FROM items tb
LEFT JOIN itemCode cod ON tb.ID = cod.codeID
JOIN tblC stock ON stock.`stockID` = cod.`availabilityID`
WHERE tb.product = :product AND cod.name = :cName
I have two tables, 1st table holds the categories list and 2nd table holds the profile information along with 1st table category id as a foreign key. As follow
Table1
id CATEGORY
1 first
2 second
Table2
id CATEGORY name phone
1 2 John 9999999999
how to retrieve table 2 records along with category name (not id:2 as shown in table 2)
I tried this,
SELECT category, name, phone FROM table2;
I need to see following line as result
second, john, 9999999999
get me out of this step, thanks in advance.
What you need is a JOIN, which means you "combine" two tables by linking rows from one table to rows from another table based on some criterion.
In your case, the criterion is that the value of CATEGORY in table2 must equal the value of ID in table1, which is expressed as follows:
SELECT table1.category,
table2.name,
table2.phone
FROM table2
JOIN table1
ON table2.category = table1.id
If needed, you can add a WHERE clause to limit the result to specific rows, e.g. WHERE table1.id = 9999999999 would filter the rows that have category 9999999999.
This should work:
SELECT t1.category, t2.name, t2.phone
FROM table2 AS t2
JOIN table1 AS t1
ON t1.id = t2.category
You can make an INNER JOIN and get the category from the Table 1, like this:
SELECT tb1.category, tb2.name, tb2.phone
FROM table2 tb2
INNER JOIN table1 tb1 on tb2.category = tb1.id
WHERE tb2.id = 1;
Hope it helps!