0

This problem is eating up my brain for the past few hours.

I have 2 tables:

**domain_pricing**
action  enum()
map varchar(10)
cost_price  varchar(10)
sale_price  varchar(10) 

**domain_mapping**
map varchar(10)
tld varchar(10)

map is common for both tables. sample data:

**domain_pricing**
addnewdomain,dotbiz,12,13
renewdomain,dotbiz,12,13
transferdomain,dotbiz,12,13

**domain_mapping**
dotbiz,biz
dotbiz,fizz
dotbiz,jizz

what i need after join:

biz,addnewdomain,12,13
biz,renewdomain,12,13
biz,transferdomain,12,13
fizz,addnewdomain,12,13
fizz,renewdomain,12,13
fizz,transferdomain,12,13
jizz,addnewdomain,12,13
jizz,renewdomain,12,13
jizz,transferdomain,12,13

my query:

select m.tld,p.action,p.sales_price,p.cost_price from domain_pricing as p, domain_mapping as m where p.map=m.map

result: biz,addnewdomain,12,13 biz,renewdomain,12,13 biz,transferdomain,12,13 dats all, tried doing left join but that gave all values for biz then fizz,null,null and finally jizz,null,null

Can't figure out where i'm going wrong

4

3 回答 3

1

当我看到新用户或新用户的教程使用,for 连接时,我总是感到畏缩,因为它通常会产生他们不期望的结果。我非常相信明确说明您正在寻找的加入类型,以便其他人清楚地了解您的意图。尝试这个:

SELECT m.tld,p.action,p.sales_price,p.cost_price
FROM domain_pricing AS p
INNER JOIN domain_mapping AS m WHERE p.map=m.map
于 2013-01-18T16:59:44.803 回答
1

我认为解决了你的问题。最好的部分是您的查询没有任何问题。;-) 您只是在查询中打错了字。这发生在每个人身上。

从 domain_pricing 中选择 m.tld,p.action,p.sale* s *_price,p.cost_price 作为 p,domain_mapping 作为 m 其中 p.map=m.map

它应该是:

select m.tld,p.action,p.sale_price,p.cost_price from domain_pricing as p, domain_mapping as m where p.map=m.map

不同之处在于您查询查找不存在的列 sale* s *。您想要的栏目是销售。

    CREATE TABLE domain_pricing (
  action enum('addnewdomain','renewdomain','transferdomain')  primary key,
  map varchar(10),
  cost_price  varchar(10),
  sale_price  varchar(10)
  );

CREATE TABLE domain_mapping(
  map varchar(10),
  tld varchar(10)
  );

INSERT INTO domain_pricing (action, map, cost_price, sale_price)
VALUES 
('addnewdomain','dotbiz',12,13),
('renewdomain','dotbiz',12,13),
('transferdomain','dotbiz',12,13);

INSERT INTO domain_mapping (map,tld)
VALUES 
('dotbiz','biz'),
('dotbiz','fizz'),
('dotbiz','jizz');

SELECT * FROM domain_pricing;

SELECT * FROM domain_mapping;

select m.tld,p.action,p.sale_price,p.cost_price from domain_pricing as p, domain_mapping as m where p.map=m.map;


select m.tld,p.action,p.cost_price, p.sale_price, from domain_pricing as p, domain_mapping as m where p.map=m.map;

在行动中看到它:http ://www.sqlfiddle.com/#!2/f6b83/7

顺便说一句,我不确定您为什么需要 enum 才能采取行动?我建议改为使用整数列作为主键。

于 2013-01-19T14:53:34.083 回答
0

要获得您描述的结果,您需要一个 CROSS JOIN。

SELECT m.tld, p.action, p.cost_price, p.sale_price
FROM domain_mapping m
CROSS JOIN domain_pricing p

这称为笛卡尔积,您故意省略连接条件,以便将一个表中的所有行映射到另一个表的所有行。MySQL 也将此称为完全连接

于 2013-01-18T16:57:26.203 回答