0

I have a table products and another table categories.
Here is some sample data
Products

id    |  product_name      |  category_id
1     |  Leath Machine     |  1
2     |  Drilling Machine  |  1
3     |  Boring Machine    |  1

And here is category table

id    |   name           | symbol
1     |   SMT Equipment  | SMT

I want to do a select which will display me results like this

id    |  product_name      |  category_id  |  code
1     |  Leath Machine     |  1            |  SMT00001
2     |  Drilling Machine  |  1            |  SMT00002
3     |  Boring Machine    |  1            |  SMT00003

How cai do that.

4

3 回答 3

3
SELECT
  `products`.`id`,
  `product_name`,
  `category_id`,
  CONCAT(symbol, LPAD(products.id,5,'0')) AS code
FROM products
  INNER JOIN categories
    ON products.category_id = categories.id;

Here is the SQLFiddle results.

SQL Fiddle Demo

于 2013-02-21T20:11:31.717 回答
1

try this

   select p.id,p.product_name,p.category_id,concat(symbol ,'0000',p.id) code from Products p
   inner join category c
   on p.category_id = c.id

DEMO SQLFIDDLE HERE

于 2013-02-21T20:07:28.797 回答
0

SELECT p.*, CONCAT(c.symbol, LPAD(p.id, 5, '0')) FROM products p INNER JOIN category c ON p.category_id=c.id;

于 2013-02-21T20:09:53.207 回答