Shantanu's Blog

Database Consultant

January 29, 2008

 

MySQL Case Study - 165

I have a column named "path" containing web urls and I'd like to extract the file extension from those that have one (some are folder links) and place it

into a column named "ext" for quick sorting of the content and I was wondering how I'd construct a query to do that for me. In the future, I'll have my PHP

script do it as the urls are added but for now I have a quite a bit of legacy content to sort through.

Ans:

select url,
lower(
substring_index(
substring_index(
case
when locate('.',substring_index(url, '/', -1)) <> 0 then substring_index(url, '/', -1)
else '.'
end,
'.', -1),
'?', 1)
) from yourtable

Hope this helps :)

obviously, you need to convert it into an update statement,

http://forums.mysql.com/read.php?10,190785,190800#msg-190800

_____


I have to implement something which apparently looks pretty simple, but in fact it is not.

Let assume the table : MYTABLE with 2 columns : A and B

For example :

A B
10 20
12 14
12 20
15 20
10 30
12 30
18 40
15 25

I'd like to find values of B where A=10 and A=12 AND A=15 for the same value of B. In my example it would return only B=20.

Is there a simple way of doing this ? I tried using subqueries, but I failed.

Ans:

SELECT b FROM mytable
WHERE a=10 OR a=12 OR a = 15
GROUP BY b
HAVING COUNT(a) = 3
HAVING COUNT( DISTINCT a ) = 3;

http://forums.mysql.com/read.php?10,190709,190720#msg-190720

_____

I have been playing with this query for a while and can't get the results i want. can somone help me.

here is my Query:

SELECT (ps.round1+ps.handicap) AS rd1
FROM userchoice uc
JOIN players p
ON uc.playerId=p.playerId
JOIN playerscores ps
ON ps.playerId=uc.playerId AND ps.tournamentId=uc.tourneyId AND ps.round1!='0'
WHERE uc.username='billy' AND uc.entryId=1
ORDER BY rd1 ASC
LIMIT 4

Results:
Rd1
60
65
68
69

Now, how can i modify this query so that it adds those 4 numbers together and returns the total from the four (which in this case would be 262)

I tried throwing a SUM(ps.round1+ps.handicap) AS rd1 in there, but that gave me a value of 497.

Ans:

SELECT SUM(rd1)
FROM (
SELECT ps.round1+ps.handicap AS rd1
FROM userchoice uc
JOIN players p
ON uc.playerId=p.playerId
JOIN playerscores ps ON ps.playerId=uc.playerId AND ps.tournamentId=uc.tourneyId AND ps.round1!='0'
WHERE uc.username='billy' AND uc.entryId=1
ORDER BY rd1 ASC LIMIT 4
) AS tmp;


http://forums.mysql.com/read.php?10,190447,190458#msg-190458

_____

Can someone please help me with an sql query to filter duplicated rows in a table. The problem is that sometimes row1 and row2 fields are reversed but this is something that i would like removed from the table.

name1 | name2
-----------------
John | Jane
Fred | Claudia
Jim | Claire
Jane | John
Claire | Jim

So if rows where name1/name2=name2/name1 then one of these rows should be filtered. i.e if John/Jane appears on one row then Jane/john should be removed from the table in the other row.

Sounds easy but i cannot find a way to do it, any ideas gurus out there?

Ans:

DELETE n1.* FROM names AS n1,names AS n2 WHERE n1.name1 = n2.name2 AND n2.name1 = n1.name2;


http://forums.mysql.com/read.php?10,190338,190345#msg-190345

_____

Other notable threads are as follows:
1) MySQL interview questions...

http://forums.mysql.com/read.php?10,191044,191044#msg-191044

2) Encrypted passwords
http://forums.mysql.com/read.php?10,190993,190993#msg-190993

3) Full Text Search
http://forums.mysql.com/read.php?10,190979,190979#msg-190979

http://forums.mysql.com/read.php?10,193470,193470#msg-193470

4) Subquery Optimization

http://forums.mysql.com/read.php?10,192870,192891#msg-192891

http://forums.mysql.com/read.php?10,192503,192511#msg-192511

Labels:


Comments: Post a Comment

<< Home

Archives

June 2001   July 2001   January 2003   May 2003   September 2003   October 2003   December 2003   January 2004   February 2004   March 2004   April 2004   May 2004   June 2004   July 2004   August 2004   September 2004   October 2004   November 2004   December 2004   January 2005   February 2005   March 2005   April 2005   May 2005   June 2005   July 2005   August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   May 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006   December 2006   January 2007   February 2007   March 2007   April 2007   June 2007   July 2007   August 2007   September 2007   October 2007   November 2007   December 2007   January 2008   February 2008   March 2008   April 2008   July 2008   August 2008   September 2008   October 2008   November 2008   December 2008   January 2009   February 2009   March 2009   April 2009   May 2009   June 2009   July 2009   August 2009   September 2009   October 2009   November 2009   December 2009   January 2010   February 2010   March 2010   April 2010   May 2010   June 2010   July 2010   August 2010   September 2010   October 2010   November 2010   December 2010   January 2011   February 2011   March 2011   April 2011   May 2011   June 2011   July 2011   August 2011   September 2011   October 2011   November 2011   December 2011   January 2012   February 2012   March 2012   April 2012   May 2012   June 2012   July 2012   August 2012   October 2012   November 2012   December 2012   January 2013   February 2013   March 2013   April 2013   May 2013   June 2013   July 2013   September 2013   October 2013   January 2014   March 2014   April 2014   May 2014   July 2014   August 2014   September 2014   October 2014   November 2014   December 2014   January 2015   February 2015   March 2015   April 2015   May 2015   June 2015   July 2015   August 2015   September 2015   January 2016   February 2016   March 2016   April 2016   May 2016   June 2016   July 2016   August 2016   September 2016   October 2016   November 2016   December 2016   January 2017   February 2017   April 2017   May 2017   June 2017   July 2017   August 2017   September 2017   October 2017   November 2017   December 2017   February 2018   March 2018   April 2018   May 2018   June 2018   July 2018   August 2018   September 2018   October 2018   November 2018   December 2018   January 2019   February 2019   March 2019   April 2019   May 2019   July 2019   August 2019   September 2019   October 2019   November 2019   December 2019   January 2020   February 2020   March 2020   April 2020   May 2020   July 2020   August 2020   September 2020   October 2020   December 2020   January 2021   April 2021   May 2021   July 2021   September 2021   March 2022   October 2022   November 2022   March 2023   April 2023   July 2023   September 2023   October 2023   November 2023  

This page is powered by Blogger. Isn't yours?