Saturday, August 9, 2008

Complicated Queries in MySQL

SELECT match_id AS m1
FROM `match_info`
ORDER BY match_id =18 DESC

7 comments:

Unknown said...

The Sharing PHP Knowledge blogspot seems to be too good for sharing individual ideas.

I would suggest you to kindly give more user friendly discussions

say for e.g., "Complicated Queris In MSQL" -- title is good.

1. Need some description for the query that's written.

2. Explanation of the query in brief what it does

etc.

Note: I didn't mean to give for each and every post. Only for Complicated logic.

Regards

GIRISHANKAR R

phpexpert said...

hai giri,

you are abolutely right, in some difficult scripts should need a minimal discription,

actually how the query is,

SELECT match_id AS m1
FROM `match_info`
ORDER BY match_id =18 DESC

in this query "match_id=18" is given in the ORDER BY because when we need to fetch the "match_id=18" as the first record, then we can write the query like this and rest of the records in the table "match_info" are fetched as 2nd , 3rd and etc records ..

Your friendly,
manoilayans

dreamcoincollection said...

Hai all,

SELECT *
FROM `zen_tax_rates`
ORDER BY tax_rates_id = (
SELECT tax_rates_id
FROM zen_tax_rates
WHERE tax_description
LIKE '%sample%'
LIMIT 1 ) ASC

by this query the selection field value in the ORDER clause is chosen by dynamic using the WHERE clause value and ASC is used here because the selection value is need to be displayed as last record in the fetch. If we want to display the selection record need to be displayed a first record in the fetch we need simply to put DESC in the ORDER BY in this query.

Regards,
yourfriend

Narasing said...

What a query? i have surprised when i was looked this query!!!!!!!

Amazing!!!! I m very feel to say this.. what can i do ?

Narasing said...

Hello PHP guys, plz look at this query, Definitely u have gathering lot knowledge....What a thinking

phpexpert said...

Sample Queries given by Kamaldassan, SDI, MySQL Guru 1. To show the firstname, middlename, lastname as a fullname

SELECT CONCAT (`first_name`, ' ', `middle_name`, ' ', `last_name`) AS fullname FROM `student_users` ORDER BY fullname ;

2. Add 31 days to the date

SELECT ADDDATE( '1998-02-28', INTERVAL 31 DAY ) OR
SELECT ADDDATE('1998-01-02', 31);

3. Add Time
SELECT ADDTIME('01:00:00.999999', '02:00:00.999998');

4. Differentiate two dates
SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');

5. Extract the year from date
SELECT EXTRACT(YEAR FROM '1999-07-02');

6. Return as time for the given second
SELECT SEC_TO_TIME(2378);


7. Find the age
SELECT name, birth, CURDATE(), (YEAR(CURDATE())-YEAR(birth)) - (RIGHT(CURDATE(),5) < RIGHT(birth,5)) AS age FROM pet ORDER BY name;

8. Find name starts with either of the following words ex: b or s or p
Now we want to select the persons with a last name that starts with "b" or "s" or "p" from the "Persons" table.

SELECT * FROM Persons WHERE LastName LIKE '[bsp]%'

The result-set will look like this:

P_Id LastName FirstName Address City
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

Anonymous said...

Get next top value in a field.

If a field having the values with prefixed by "CAT" ex: CAT1, CAT45 etc.


SELECT max( cast( substring( categoryid, 4 ) AS decimal ) ) +1
FROM `category`

This query split the "CAT" and find the next top value.

Popular Posts