Querying the pinyin initials of Chinese characters in MySQL database

Source: Internet
Author: User

The method I provide has the following characteristics:

1. Code simplification, simple to use, as long as the basic SQL statement will be the line
2. No need to build MySQL functions and other complex things
3. The most complete Chinese character library, can query 20,902 characters

Here's how:

1. Establishment of pinyin first letter data sheet
SQL code: (preferably plus primary key and index)

  1. DROP TABLE IF EXISTS ' Pinyin ';
  2. CREATE TABLE ' Pinyin ' (
  3. ' PY ' varchar (1),
  4. ' HZ1 ' varchar (1),
  5. ' HZ2 ' varchar (1)
  6. ) ;
  7. INSERT into ' pinyin ' (' PY ', ' HZ1 ', ' HZ2 ') VALUES
  8. (' A ',' acridine ',' 驁 '),
  9. (' B ',' eight ',' book '),
  10. (' C ',' cha ',' wrong '),
  11. (' D ',' Otah ',' 鵽 '),
  12. (' E ',' Ehegan ',' 樲 '),
  13. (' F ',' fa ','鰒 '),
  14. (' G ',' 猤 ',' hiker '),
  15. (' H ',' 妎 ',' inceѕt '),
  16. (' J ',' no ',' 攈 '),
  17. (' K ',' ka ',' 穒 '),
  18. (' L ','garbage ',' 鱳 '),
  19. (' M ',' 嘸 ',' temperature '),
  20. (' N ',' cabal ',' 桛 '),
  21. (' O ',' Oh ',' 漚 '),
  22. (' P ',' 妑 ',' exposure '),
  23. (' Q ',' seven ',' 囕 '),
  24. (' R ',' 呥 ',' 鶸 '),
  25. (' S ',' sa',' 蜶 '),
  26. (' T ',' he ',' 籜 '),
  27. (' W ',' 屲 ',' clamoring '),
  28. (' X ',' XI ',' 鑂 '),
  29. (' Y ',' ya ',' Wan Leng '),
  30. (' Z ',' as','n ');

after execution, check to see if the data records in the added table have "? The question mark, if any, indicates a problem with the database encoding.
2, query the database table in the first character phonetic Alphabet
SELECT ' Pinyin '. py, ' corresponding table '. *
From ' corresponding table ', ' Pinyin '
WHERE ' Kanji title ' >= ' HZ1 '
and ' Kanji title ' <= ' HZ2 ';
You can also directly query the first character of Chinese characters Phonetic Alphabet
SELECT ' pinyin '. Py
From ' Pinyin '
WHERE ' kanji ' >= ' hz1 '
and ' kanji ' <= ' hz2 ';
The query result is "H"

* * * * * Note
This method is only applicable to GBK encoding, the other encoding needs to be converted to GBK first, can be used with convert (words using GBK)

you can also use functions to handle
CREATEFUNCTIONfirstpy (wordsvarchar (255))RETURNSMediumtext
 BEGIN
           Declarefpychar (1);
           DeclarePCchar (1);
           Declareccchar (4);
           Set@fpy =  UPPER (Left (words,1));
           Set@pc =  (CONVERT (@fpyUSINGGBK));
           Set@cc =  Hex (@pc);
           if@cc>="8140" and@cc<= "FEA0" Then
                     begin
                               Selectpy into@fpy fromPinyinwhereHz2>[email protected]and Hz1<[email protected];
                     end;
           Endif;
           Return@fpy;
 END

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.