如何创建sql语句以获得以下结果

时间:2013-08-19 15:36:02

标签: php mysql sql

我的数据库中有以下表格

表名是“product_question”

product_question_id int(11) No       
product_id  int(11) No       
user_name   varchar(100)    No       
user_email  varchar(200)    No       
envipor varchar(100)    No       
company_name    varchar(100)    No       
product_name    varchar(100)    No       
question    text    No       
product_image   varchar(500)    No       
date    timestamp   No  CURRENT_TIMESTAMP 

我有以下sql语句

我的.php页面中的判刑SQL就是

SELECT*
FROM product_question
WHERE company_name = $_SESSION['company_name']

但我希望将.php页面上的结果转换成以这种方式显示的简单语句

product: xbox 360
question: what is the price? - user_name: brian

product: ps3
question: has availability? - user_name: carlos

product: xbox 360
question: has games? - michael

product: ps3
question: what is the price? - user_name: luke

product:  smartphone
question : brings android? - selena

product: xbox 360
question: how many games? - user_name: daniel

我希望句子的结果以这种方式显示

产品:xbox 360

question: what is the price? - user_name: brian
question: has games? - michael
question: how many games? - user_name: daniel

产品:ps3

question: has availability? - user_name: carlos
question: what is the price? - user_name: luke

产品:智能手机

question : brings android? - selena

但是,我不知道创建sql语句以获得所需的结果。

1 个答案:

答案 0 :(得分:0)

如评论中所述,以您需要的格式从数据库获取结果的方法会发出如下所示的查询。这假设你使用的是mysqli的程序风格。

$sql = "select CONCATENATE('question: ', question, ' - ', user_name) as questionText ".
       "from product_question ".
       "WHERE company_name = '".mysqli_real_escape_string($dbConnection, $_SESSION['company_name'])."'";