MYSQL:显示寄存器,关联和重命名

时间:2013-05-07 16:02:28

标签: mysql

说明如下:

生成客户购买的所有产品的报告:客户的ID,客户的全名,城市,州,ID号,销售日期,产品代码,产品名称,销售数量以及最后显示“已付款”或“付款待处理”状态的消息,具体取决于付款状态,其中0 =付款且1 =待处理。此报告应首先按州按字母顺序排序,然后按客户名称排序。

我试过的是:

    select cli_nom, cli_city, cli_state, fac_num, fac_saledate, prod_cod, fac_total, fac_status 
where fac_status = 0 as paid and fac_status = 1 as pending 
from factures, products, clients order by cli_state, cli_nom, asc;

绝对没有absolutley工作,我不确定重命名或掩盖列的sintax。

表格结构如下:

table clientes:

 1. cli_nom  varchar(100)
 2. cli_state varchar(100)
 3. cli_city varchar(100)
 4. cli_id int(11)
 5. cli_status int(11)
 6. cli_dateofsale date

table products:

 1. prod_cod int(11)
 2. prod_categ char(1)
 3. prod_nom varchar(100)
 4. prod_price double
 5. prod_descrip varchar(100)
 6. prod_discount float

table facturas:

 1. fac_num int(11)
 2. fac_datesold date
 3. fac_cli_id int(11)
 4. fac_status int
 5. fac_total float

1 个答案:

答案 0 :(得分:1)

您遇到查询问题。 当你想查询某些东西时,完整陈述的形式是这样的

Select [fields]
from [table(s)] --which means there includes inner joins
where [filter rows]
group by [fields to group]
having [filtering groups]
order by [fields]

当然,这是一个比这更复杂和更大的东西,但它会给你一些初步的概念。

您将始终遵守此订单,因此在您的查询中将选择放在哪里。

如果你想改变要显示的东西,取决于某些评估,但你总是会显示某些内容(你没有过滤,你选择根据值显示什么),你可以使用CASE子句

在这个例子中,你可以做这样的事情

select cli_nom, cli_city, cli_state, fac_num, fac_saledate, 
prod_cod, fac_total, fac_status 
CASE when fac_status = 0  then 'You Paid'
     when fac_status = 1 then 'payment Pending'
     else 'Not sure about state' END
from factures
inner join products on --put here how do you relate products with factures
inner join clients on -- put here how do you relate clients with products/factures
order by cli_state, cli_nom, asc;

如果你不知道如何使用INNER JOIN,here你有一些信息。

基本上,是一个用于关联两个表的子句。

类似

(..)
from Table1 A
INNER JOIN Table2 B on A.id = B.id

(A和B是别名,表示已设置的表)。

这意味着它会将Table1中的每一行与Table2中的每一行进行比较,并且当条件匹配时(在这种情况下,来自table1的{[{1}}]的id等于来自Table2 [A.id)的id然后显示关系行(表示它将显示table1中的所有行+ table2中的所有行)