开发者

mysql Using INNER JOIN to select multiple tables?

开发者 https://www.devze.com 2023-02-24 04:43 出处:网络
I want to select all fields of several table and fetch result separate but mysql return all rows together:

I want to select all fields of several table and fetch result separate but mysql return all rows together:

SELECT prod_product.*
  ,pub_comment.* 
FROM prod_product 
INNER JOIN pub_comment ON (prod_product.id = pub_comment.itemId) 
WHERE prod_product.id=7744

Is there any way that i could fetch each table rows sepa开发者_C百科rately?

I try @prod:=prod_product.*, @comment:=pub_comment.* but mysql didn't allow me to store more than 1 row.


Execute two queries:

select * from prod_product WHERE prod_product.id=7744

and

select * from pub_comment WHERE pub_comment.itemId=7744

A single query always return single rows containing two table fields.
Anyway: what is the problem of having columns together in a single row? If you have problems with their names you can use aliases.


SELECT @prod:= CONCAT_WS(',',prod_product.field1,prod_product.field2,...)
  ,@comment:= CONCAT_WS(' ',pub_comment.field1,pub_comment.field2,....) 
FROM prod_product 
INNER JOIN pub_comment ON (prod_product.id = pub_comment.itemId) 
WHERE prod_product.id = 7744

Note that CONCAT_WS will separate your fields and CONCAT will just smash then together.

0

精彩评论

暂无评论...
验证码 换一张
取 消