过年后论坛最高分,求简精SQL语句
table名wc
id int er sku qty
339 2012020070 8438453 77735041-1/4 27
440 2012020071 8438453 77735041-2/4 84
441 2012020072 8438453 77735041-3/4 27
442 2012020073 8438453 77735041-4/4 27
443 2012020074 8438450 77735041-1/4 27
444 2012020075 8438450 77735041-2/4 27
445 2012020076 8438450 77735041-3/4 27
446 2012020077 8438450 77735041-4/4 27
447 2012020078 8430730 20095101-1/4 27
448 2012020079 8430730 20095101-2/4 27
449 2012020080 8430730 20095101-3/4 27
450 2012020081 8430730 20095101-4/4 90
451 2012020082 8430731 20095105 30
452 2012020083 8430732 20095100 30
453 2012020084 8430734 20095107-1/2 30
454 2012020085 8430734 20095107-2/2 30
看此:
sku列,有的值只有一个如20095105;有的2个,但后边有分数注明了如20095100;有的有4个,但int值又不一样,即有多组,如77735041
我要实现sql语句,
1。查询条件是sku值,前8位。如:left(sku,8)=77735041,20095107.并不是输入”77735041-1/4“,而是输入77735041;
2。实现功能:查找出,输入条件的相对应的第一组数据。
比如,我输入77735041,得到如下(注意是共有2组数据,但只要第一组)
339 2012020070 8438453 77735041-1/4 27
440 2012020071 8438453 77735041-2/4 84
441 2012020072 8438453 77735041-3/4 27
442 2012020073 8438453 77735041-4/4 27
比如,我输入20095100,得到如下
452 2012020083 8430732 20095100 30
比如,我输入20095107,得到如下
453 2012020084 8430734 20095107-1/2 30
454 2012020085 8430734 20095107-2/2 30
------解决方案--------------------
SQL code
SELECT
wc.*
FROM
wc,
(SELECT
MIN(IF(c = '1/', id, NULL)) idMin,
MIN(IF(c = '/', id, NULL)) idMax,
IF(c = '', id, NULL) only
FROM
(SELECT
REPLACE(
SUBSTR(sku, LOCATE('-', sku) + 1),
SUBSTR(sku, LOCATE('/', sku) + 1),
''
) c,
wc.id
FROM
wc
WHERE sku LIKE '20095107%') t) t
WHERE wc.id = t.only
OR wc.id BETWEEN t.idMin
AND t.idMax
------解决方案--------------------
SQL code
SELECT * FROM wc w
WHERE er=(SELECT MIN(er) FROM wc WHERE LEFT(sku,8)='20095105')
------解决方案--------------------
SQL code
SELECT * FROM wc w,(SELECT MIN(er) er FROM wc WHERE LEFT(sku,8)='20095105')t
WHERE w.er=t.er
------解决方案--------------------
select * from wc w
where er=(select min(er) from wc where left(sku,8)='77735041')
------解决方案--------------------
SELECT * FROM wc w,(SELECT MIN(er) er FROM wc WHERE LEFT(sku,8)='20095105')t
WHERE w.er=t.er