找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1274|回复: 5

[分享] SQL常用命令

[复制链接]

已领礼包: 20个

财富等级: 恭喜发财

发表于 2021-2-1 21:15:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

  1. USE sql_store;

  2. -- >>>>>>> The SELECT, WHERE Clause <<<<<<<
  3. SELECT
  4.         first_name,
  5.     last_name,
  6.     points,
  7.     (points + 10) * 100 AS 'discount factor'
  8. FROM customers
  9. -- WHERE customer_id = 1
  10. -- ORDER BY first_name
  11. ;

  12. -- >>>>>>> The AND, OR and NOT Operators <<<<<<<
  13. SELECT *
  14. FROM customers
  15. WHERE birth_date > '1990-01-01' OR (points > 1000 AND state = 'VA');
  16. -- WHERE birth_date > '1990-01-01' OR points > 1000 AND state = 'VA'

  17. SELECT *
  18. FROM customers
  19. WHERE NOT (birth_date > '1990-01-01' OR points > 1000);


  20. SELECT *
  21. FROM customers
  22. WHERE birth_date <= '1990-01-01' AND points <= 1000;

  23. -- >>>>>>> The IN Operato <<<<<<<
  24. SELECT *
  25. FROM customers
  26. WHERE state IN ('VA','FL','GA')
  27. -- WHERE state = 'VA' OR state = 'GA' OR state = 'FL'
  28. -- WHERE state NOT IN ('VA','FL','GA')
  29. ;

  30. -- >>>>>>> The BETWEEN Operator <<<<<<<
  31. SELECT *
  32. FROM customers
  33. WHERE points BETWEEN 1000 AND 3000
  34. -- WHERE points >= 1000 AND points <= 3000
  35. ;

  36. -- >>>>>>> The LIKE Operator <<<<<<<
  37. SELECT *
  38. FROM customers
  39. WHERE last_name LIKE 'b%' ## last name start with 'b/B'
  40. -- WHERE last_name LIKE '%b%'
  41. -- WHERE last_name LIKE '%y'
  42. ;

  43. SELECT *
  44. FROM customers
  45. WHERE last_name LIKE 'b____y'
  46. -- % any number of characters
  47. -- _ single character
  48. ;

  49. -- >>>>>>> The REGEXP Operator <<<<<<<
  50. SELECT *
  51. FROM customers
  52. WHERE last_name REGEXP 'field$|mac|rose'
  53. -- WHERE last_name REGEXP '^field'
  54. -- WHERE last_name REGEXP 'field

  55. -- WHERE last_name REGEXP 'field|mac'
  56. -- WHERE last_name REGEXP '[gim]e'
  57. -- ^ beginning
  58. -- $ end
  59. -- | logical or
  60. -- [abcd]
  61. -- [a-f]
  62. ;
  63. -- >>>>>>> The IS NULL Operator <<<<<<<
  64. SELECT *
  65. FROM customers
  66. WHERE phone IS NOT NULL
  67. -- WHERE phone IS NULL
  68. ;

  69. -- >>>>>>> The ORDER BY Clause <<<<<<<
  70. SELECT *
  71. FROM customers
  72. ORDER BY state DESC, first_name DESC
  73. -- ORDER BY state, first_name
  74. ;

  75. SELECT first_name, last_name, 10 AS points
  76. FROM customers
  77. ORDER BY points, first_name
  78. ;

  79. SELECT first_name, last_name, 10 AS points
  80. FROM customers
  81. ORDER BY 1, 2
  82. ;

  83. -- >>>>>>> The LIMIT Clause <<<<<<<
  84. SELECT *
  85. FROM customers
  86. LIMIT 3
  87. ;

  88. SELECT *
  89. FROM customers
  90. LIMIT 6, 3
  91. -- page 1: 1 - 3
  92. -- page 2: 4 - 6
  93. -- page 3: 7 - 9
  94. ;
  95. -- >>>>>>> Inner Joins <<<<<<<
  96. SELECT order_id, o.customer_id, first_name, last_name
  97. FROM orders o
  98. JOIN customers c
  99.         ON o.customer_id = c.customer_id
  100. ;
  101. -- >>>>>>> Joining Across Databases <<<<<<<
  102. SELECT *
  103. FROM order_items oi
  104. JOIN sql_inventory.products p
  105.         ON oi.product_id = p.product_id
  106. ;
  107. USE sql_inventory;

  108. SELECT *
  109. FROM sql_store.order_items oi
  110. JOIN products p
  111.         ON oi.product_id = p.product_id
  112. ;
  113. -- >>>>>>> Self Joins <<<<<<<
  114. USE sql_hr;

  115. SELECT
  116.         e.employee_id,
  117.     e.first_name,
  118.     m.first_name AS manager
  119. FROM employees e
  120. JOIN employees m
  121.         ON e.reports_to = m.employee_id
  122. ;
  123. -- >>>>>>> Joining Multiple Tables <<<<<<<
  124. USE sql_store;

  125. SELECT
  126.         o.order_id,
  127.     o.order_date,
  128.     c.first_name,
  129.     c.last_name,
  130.     os.name AS status
  131. FROM orders o
  132. JOIN customers c
  133.         ON o.customer_id = c.customer_id
  134. JOIN order_statuses os
  135.         ON o.status = os.order_status_id
  136. ;
  137. -- >>>>>>> Compound Join Conditions <<<<<<<
  138. USE sql_store;

  139. SELECT *
  140. FROM order_items oi
  141. JOIN order_item_notes oin
  142.         ON oi.order_id = oin.order_id
  143.         AND oi.product_id = oin.product_id
  144. ;
  145. -- >>>>>>> Implicit Join Syntax <<<<<<<
  146. SELECT *
  147. FROM orders o
  148. JOIN customers c
  149.         ON o.customer_id = c.customer_id
  150. ;
  151. SELECT *
  152. FROM orders o, customers c
  153. WHERE o.customer_id = c.customer_id  ## euqal to the previous one
  154. ;
  155. -- >>>>>>> Outer Joins <<<<<<<
  156. SELECT
  157.         c.customer_id,
  158.     c.first_name,
  159.     o.order_id
  160. FROM customers c
  161. JOIN orders o
  162.         ON c.customer_id = o.customer_id
  163. ORDER BY c.customer_id
  164. ;

  165. SELECT
  166.         c.customer_id,
  167.     c.first_name,
  168.     o.order_id
  169. FROM customers c
  170. LEFT JOIN orders o
  171.         ON c.customer_id = o.customer_id
  172. ORDER BY c.customer_id
  173. ;

  174. SELECT
  175.         c.customer_id,
  176.     c.first_name,
  177.     o.order_id
  178. FROM orders o
  179. RIGHT JOIN customers c
  180.         ON c.customer_id = o.customer_id
  181. ORDER BY c.customer_id
  182. ;
  183. -- >>>>>>> Outer Join Between Multiple Tables <<<<<<<
  184. SELECT
  185.         c.customer_id,
  186.     c.first_name,
  187.     o.order_id,
  188.     sh.name AS shipper
  189. FROM customers c
  190. LEFT JOIN orders o
  191.         ON c.customer_id = o.customer_id
  192. LEFT JOIN shippers sh
  193.         ON o.shipper_id = sh.shipper_id
  194. ORDER BY c.customer_id
  195. ;
  196. -- >>>>>>> Self Outer Joins <<<<<<<
  197. USE sql_hr;

  198. SELECT
  199.         e.employee_id,
  200.     e.first_name,
  201.     m.first_name AS manager
  202. FROM employees e
  203. LEFT JOIN employees m
  204.         ON e.reports_to = m.employee_id
  205. ;
  206. -- >>>>>>> The USING Clause <<<<<<<
  207. USE sql_store
  208. ;
  209. SELECT
  210.         o.order_id,
  211.     c.first_name,
  212.     sh.name AS shipper
  213. FROM orders o
  214. JOIN customers c
  215.         USING (customer_id)
  216. LEFT JOIN shippers sh
  217.         USING (shipper_id)
  218. ;
  219. SELECT *
  220. FROM order_items oi
  221. LEFT JOIN order_item_notes oin
  222.         USING (order_id, product_id)
  223. ;
  224. -- >>>>>>> Natural Joins <<<<<<<
  225. USE sql_store;
  226. SELECT *
  227. FROM orders o
  228. NATURAL JOIN customers c
  229. ;
  230. -- >>>>>>> Cross Joins <<<<<<<
  231. SELECT
  232.         c.first_name AS customer,
  233.     p.name AS product
  234. FROM customers c
  235. CROSS JOIN products p
  236. ORDER BY c.first_name
  237. ;

  238. SELECT
  239.         c.first_name AS customer,
  240.     p.name AS product
  241. FROM customers c, products p
  242. ORDER BY c.first_name
  243. ;
  244. -- >>>>>>> Unions <<<<<<<
  245. SELECT
  246.         order_id,
  247.     order_date,
  248.     'Active' AS status
  249. FROM orders
  250. WHERE order_date >= '2019-01-01'
  251. UNION
  252. SELECT
  253.         order_id,
  254.     order_date,
  255.     'Archived' AS status
  256. FROM orders
  257. WHERE order_date < '2019-01-01'
  258. ;
  259. SELECT first_name AS full_name
  260. FROM customers
  261. UNION
  262. SELECT name
  263. FROM shippers
  264. ;
  265. -- >>>>>>> Column Attributes <<<<<<<

  266. -- >>>>>>> Inserting a Row <<<<<<<
  267. INSERT INTO customers (
  268.         first_name,
  269.     last_name,
  270.     birth_date,
  271.     address,
  272.     city,
  273.     state)
  274. VALUES (
  275.     'John',
  276.     'Smith',
  277.     '1990-01-01',
  278.     'address',
  279.     'city',
  280.     'CA')
  281. ;
  282. -- >>>>>>> Inserting Multiple Rows <<<<<<<
  283. INSERT INTO shippers (name)
  284. VALUES ('Shipper1'),
  285.            ('Shipper2'),
  286.        ('Shipper3')
  287. ;
  288. -- >>>>>>> Inserting Hierarchical Rows <<<<<<<
  289. INSERT INTO orders (customer_id, order_date, status)
  290. VALUES (1, '2019-01-02', 1);

  291. INSERT INTO order_items
  292. VALUES
  293.            (LAST_INSERT_ID(), 1, 1, 2.95),
  294.        (LAST_INSERT_ID(), 2, 1, 3.95)
  295. ;
  296. -- >>>>>>> Creating a Copy of a Table <<<<<<<
  297. CREATE TABLE orders_archived AS
  298. SELECT *
  299. FROM orders
  300. ;
  301. INSERT INTO orders_archived
  302. SELECT *
  303. FROM orders
  304. WHERE order_date < '2019-01-01'
  305. ;
  306. -- >>>>>>> Updating a Single Row <<<<<<<
  307. UPDATE invoices
  308. SET payment_total = DEFAULT, payment_date = NULL
  309. WHERE invoice_id = 1
  310. ;
  311. UPDATE invoices
  312. SET
  313.         payment_total = invoice_total * 0.5,
  314.     payment_date = due_date
  315. WHERE invoice_id = 3
  316. ;
  317. -- >>>>>>> Updating Multiple Rows <<<<<<<
  318. UPDATE invoices
  319. SET
  320.         payment_total = invoice_total * 0.5,
  321.     payment_date = due_date
  322. WHERE client_id = 3 ## Preferences --> SQL Editor --> safe update
  323. -- WHERE client_id IN (3,4)
  324. ;
  325. -- >>>>>>> Using Subqueries in Updates <<<<<<<
  326. USE sql_invoicing;

  327. UPDATE invoices
  328. SET
  329.         payment_total = invoice_total * 0.5,
  330.     payment_date = due_date
  331. WHERE client_id = (
  332.                         SELECT client_id
  333.                         FROM clients
  334.                         WHERE state IN ('CA', 'NY'))
  335. ;
  336. UPDATE invoices
  337. SET
  338.         payment_total = invoice_total * 0.5,
  339.     payment_date = due_date
  340. WHERE payment_date IS NULL
  341. ;
  342. -- >>>>>>> Deleting Rows <<<<<<<
  343. USE sql_invoicing;

  344. DELETE FROM invoices
  345. WHERE client_id = (
  346.                         SELECT client_id
  347.                         FROM clients
  348.                         WHERE name = 'Myworks')
  349. ;
  350. -- >>>>>>> Restoring the Databases <<<<<<<

  351. -- >>>>>>> Aggregate Functions <<<<<<<
  352. USE sql_invoicing;

  353. SELECT
  354.         MAX(invoice_total) AS hightest,
  355.     MIN(invoice_total) AS lowest,
  356.     AVG(invoice_total) AS avverage,
  357.     SUM(invoice_total * 1.1) AS total,
  358.     Count(invoice_total) AS number_of_invoices,
  359.     Count(payment_date) AS count_of_payments,
  360.     Count(DISTINCT(client_id)) AS total_records, ## without duplication
  361.     Count(*) AS total_records1
  362. FROM invoices
  363. WHERE invoice_date > '2019-07-01'
  364. ;
  365. -- >>>>>>> The GROUP BY Clause <<<<<<<
  366. SELECT
  367.         state,
  368.     city,
  369.         SUM(invoice_total) AS total_sales
  370. FROM invoices
  371. JOIN clients
  372.         USING (client_id)
  373. WHERE invoice_date >= '2019-07-01'
  374. GROUP BY state, city
  375. ORDER BY total_sales DESC
  376. ;
  377. -- >>>>>>> The HAVING Clause <<<<<<<
  378. SELECT
  379.         client_id,
  380.         SUM(invoice_total) AS total_sales,
  381.     COUNT(*) AS number_of_invoivrs
  382. FROM invoices
  383. -- WHERE client_id <= 5 ## Here should not be aggregation function
  384. GROUP BY client_id
  385. HAVING total_sales > 500 AND number_of_invoivrs > 5## filter data after group --> aggregation function
  386. ;
  387. -- >>>>>>> The ROLLUP Operator <<<<<<<
  388. SELECT
  389.         state,
  390.     city,
  391.         SUM(invoice_total) AS total_sales
  392. FROM invoices i
  393. JOIN clients c USING (client_id)
  394. GROUP BY state, city WITH ROLLUP
  395. ;
  396. -- >>>>>>> Subqueries <<<<<<<
  397. SELECT *
  398. FROM products
  399. WHERE unit_price > (
  400.         SELECT unit_price
  401.     FROM products
  402.     WHERE product_id = 3
  403. )
  404. ;
  405. -- >>>>>>> The IN Operator <<<<<<<
  406. USE sql_store;

  407. SELECT *
  408. FROM products
  409. WHERE product_id NOT IN (
  410. SELECT DISTINCT product_id
  411. FROM order_items # product 7 has never been ordered
  412. )
  413. ;
  414. -- >>>>>>> Subqueries vs Joins <<<<<<<
  415. USE sql_invoicing;

  416. SELECT *
  417. FROM clients
  418. WHERE client_id NOT IN (
  419.         SELECT DISTINCT client_id
  420.     FROM invoices
  421. )
  422. ;

  423. SELECT *
  424. FROM clients
  425. LEFT JOIN invoices USING (client_id)
  426. WHERE invoice_id IS NULL
  427. ;
  428. -- >>>>>>> The ALL Keyword <<<<<<<
  429. USE sql_invoicing;

  430. SELECT *
  431. FROM invoices
  432. WHERE invoice_total > (
  433.         SELECT MAX(invoice_total)
  434.     FROM invoices
  435.     WHERE client_id = 3
  436. )
  437. ;
  438. -- Select invoices larger than all invoices of client 3

  439. SELECT *
  440. FROM invoices
  441. WHERE invoice_total > ALL(
  442.         SELECT invoice_total
  443.         FROM invoices
  444.         WHERE client_id = 3
  445. )
  446. ;
  447. -- >>>>>>> The ANY Keyword <<<<<<<

  448. -- SELECY clients with at least two invoices
  449. SELECT *
  450. FROM clients
  451. WHERE client_id IN (
  452.         SELECT client_id
  453.         FROM invoices
  454.         GROUP BY client_id
  455.         HAVING COUNT(*) >= 2
  456. )
  457. ;
  458. SELECT *
  459. FROM clients
  460. WHERE client_id = ANY (
  461.         SELECT client_id
  462.         FROM invoices
  463.         GROUP BY client_id
  464.         HAVING COUNT(*) >= 2
  465. )
  466. ;
  467. -- >>>>>>> Correlated Subqueries <<<<<<<

  468. -- for each employee
  469. --                 calculate the avg salary for employee.office
  470. --                 return the employee if salary > avg
  471. USE sql_hr;

  472. SELECT *
  473. FROM employees e
  474. WHERE salary > (
  475.         SELECT AVG(salary)
  476.     FROM employees
  477.     WHERE office_id = e.office_id
  478. )
  479. ;
  480. -- >>>>>>> The EXISTS Operator <<<<<<<

  481. -- Selecy clients that have an invoice
  482. SELECT *
  483. FROM clients
  484. WHERE client_id IN(
  485.         SELECT DISTINCT client_id
  486.     FROM invoices
  487. )
  488. ;
  489. SELECT *
  490. FROM clients c
  491. WHERE EXISTS(
  492.         SELECT DISTINCT client_id
  493.     FROM invoices
  494.     WHERE client_id = c.client_id
  495. )
  496. ;
  497. ## it is better to use EXISTS when it comes to great data.

  498. -- >>>>>>> Subqueries in the SELECT Clause <<<<<<<
  499. USE sql_invoicing;

  500. SELECT
  501.         invoice_id,
  502.     invoice_total,
  503.     (SELECT AVG(invoice_total)
  504.                 FROM invoices) AS invoice_average,
  505.         invoice_total - (SELECT invoice_average) AS difference
  506. FROM invoices
  507. ;
  508. -- >>>>>>> Subqueries in the FROM Clause <<<<<<<
  509. SELECT *
  510. FROM(
  511. SELECT
  512.         client_id,
  513.     name,
  514.     (SELECT SUM(invoice_total)
  515.                 FROM invoices
  516.         WHERE client_id = c.client_id) AS total_sales,
  517.         (SELECT AVG(invoice_total)
  518.                 FROM invoices) AS average,
  519.         (SELECT total_sales - average) AS difference
  520. FROM clients c
  521. ) AS sales_summary
  522. WHERE total_sales IS NOT NULL
  523. ;
  524. -- >>>>>>> Numeric Functions <<<<<<<
  525. SELECT ROUND(5.7345, 1)
  526. UNION
  527. SELECT TRUNCATE(5.7345, 2)
  528. UNION
  529. SELECT CEILING(5.7345)
  530. UNION
  531. SELECT ABS(-5.2)
  532. UNION
  533. SELECT RAND()
  534. ;
  535. -- >>>>>>> String Functions <<<<<<<
  536. SELECT LENGTH('sky')
  537. UNION
  538. SELECT UPPER('sky')
  539. UNION
  540. SELECT LOWER('SKY0')
  541. UNION
  542. SELECT LTRIM('    SKY1')
  543. UNION
  544. SELECT RTRIM('SKY2     ')
  545. UNION
  546. SELECT TRIM('    SKY3     ')
  547. UNION
  548. SELECT LEFT('Kindergarten', 4)
  549. UNION
  550. SELECT RIGHT('Kindergarten', 4)
  551. UNION
  552. SELECT SUBSTRING('Kindergarten', 3, 5)
  553. UNION
  554. SELECT LOCATE('N','Kindergarten')
  555. UNION
  556. SELECT LOCATE('garten','Kindergarten')
  557. UNION
  558. SELECT REPLACE('Kindergarten', 'garten', 'garden')
  559. UNION
  560. SELECT CONCAT('first','last')
  561. ;

  562. USE sql_store;

  563. SELECT CONCAT(first_name, ' ', last_name) AS FULL_NAME
  564. FROM customers
  565. ;
  566. -- >>>>>>> Date Functions in MySQL <<<<<<<
  567. SELECT NOW(), CURDATE(), CURTIME()
  568. ;
  569. SELECT YEAR(NOW())
  570. UNION
  571. SELECT MONTH(NOW())
  572. UNION
  573. SELECT DAY(NOW())
  574. UNION
  575. SELECT HOUR(NOW())
  576. UNION
  577. SELECT MINUTE(NOW())
  578. UNION
  579. SELECT DAYNAME(NOW())
  580. UNION
  581. SELECT MONTHNAME(NOW())
  582. ;
  583. SELECT EXTRACT(DAY FROM NOW())
  584. UNION
  585. SELECT EXTRACT(YEAR FROM NOW())
  586. ;
  587. -- >>>>>>> Formatting Dates and Times <<<<<<<
  588. SELECT DATE_FORMAT(NOW(), '%y')
  589. UNION
  590. SELECT DATE_FORMAT(NOW(), '%M %Y')
  591. ;
  592. -- >>>>>>> Calculating Dates and Times <<<<<<<
  593. SELECT DATE_ADD(NOW(), INTERVAL -1 DAY)
  594. UNION
  595. SELECT DATE_SUB(NOW(), INTERVAL 1 DAY)
  596. UNION
  597. SELECT DATEDIFF('2019-01-01 17:00','2019-01-05 09:00')
  598. UNION
  599. SELECT TIME_TO_SEC('09:00') - TIME_TO_SEC('09:02')
  600. ;
  601. -- >>>>>>> The IFNULL and COALESCE Functions <<<<<<<
  602. USE sql_store;

  603. SELECT
  604.         order_id,
  605.     IFNULL(shipper_id, 'Not assigned') AS shipper
  606. FROM orders
  607. ;
  608. SELECT
  609.         order_id,
  610.     COALESCE(shipper_id, comments, 'Not assigned') AS shipper # first is shipper_id; second is comments
  611. FROM orders
  612. ;
  613. -- >>>>>>> The IF Function <<<<<<<
  614. SELECT
  615.         order_id,
  616.     order_date,
  617.         IF(
  618.                 YEAR(order_date) = '2019-01-30',
  619.         'Active',
  620.         'Archived') AS category
  621. FROM orders
  622. ;
  623. -- >>>>>>> The CASE Operator <<<<<<<
  624. SELECT
  625.         order_id,
  626.     order_date,
  627.         CASE
  628.                 WHEN YEAR(order_date) = 2019 THEN 'Active'
  629.         WHEN YEAR(order_date) = 2019 - 1 THEN 'Last Year'
  630.         WHEN YEAR(order_date) = 2019 < 1 THEN 'Archived'
  631.         ELSE 'Future'
  632.         END AS category
  633. FROM orders
  634. ;
  635. -- >>>>>>> Creating Views <<<<<<<
  636. USE sql_invoicing;

  637. CREATE VIEW sales_by_client AS
  638. SELECT
  639.         c.client_id,
  640.     c.name,
  641.     SUM(invoice_total) AS total_sales
  642. FROM clients c
  643. JOIN invoices i USING(client_id)
  644. GROUP BY client_id, name
  645. ;
  646. SELECT *
  647. FROM sales_by_client
  648. JOIN clients USING (client_id)
  649. ORDER BY total_sales DESC
  650. ;
  651. -- >>>>>>> Altering or Dropping Views <<<<<<<
  652. DROP VIEW sales_by_client
  653. ;
  654. CREATE OR REPLACE VIEW sales_by_client AS
  655. SELECT
  656.         c.client_id,
  657.     c.name,
  658.     SUM(invoice_total) AS total_sales
  659. FROM clients c
  660. JOIN invoices i USING (client_id)
  661. GROUP BY client_id, name
  662. ;
  663. -- >>>>>>> Updatable Views <<<<<<<
  664. -- DISTINCT
  665. -- Aggregaet Functions (MIN, MAX, SUM)
  666. -- GROUP BY / HAVING
  667. -- UNION
  668. CREATE OR REPLACE VIEW invoices_with_balance AS
  669. SELECT
  670.         invoice_id,
  671.     number,
  672.     client_id,
  673.     invoice_total,
  674.     payment_total,
  675.     invoice_total - payment_total AS balance,
  676.     invoice_date,
  677.     due_date,
  678.     payment_date
  679. FROM invoices
  680. WHERE (invoice_total - payment_total) > 0
  681. ;
  682. DELETE FROM invoices_with_balance
  683. WHERE invoice_id = 1
  684. ;
  685. UPDATE invoices_with_balance
  686. SET due_date = DATE_ADD(due_date, INTERVAL 2 DAY)
  687. WHERE invoice_id = 2
  688. ;
  689. -- >>>>>>> THE WITH OPTION CHECK Clause <<<<<<<
  690. UPDATE invoices_with_balance
  691. SET payment_total = invoice_total
  692. WHERE invoice_id = 3
  693. ;
  694. CREATE OR REPLACE VIEW invoices_with_balance AS
  695. SELECT
  696.         invoice_id,
  697.     number,
  698.     client_id,
  699.     invoice_total,
  700.     payment_total,
  701.     invoice_total - payment_total AS balance,
  702.     invoice_date,
  703.     due_date,
  704.     payment_date
  705. FROM invoices
  706. WHERE (invoice_total - payment_total) > 0
  707. WITH CHECK OPTION
  708. ;
  709. -- >>>>>>> What are Stored Procedures <<<<<<<
  710. ## Store and organize SQL
  711. ## Faster execution
  712. ## Data security
  713. -- >>>>>>> Creating a Stored Procedure <<<<<<<
  714. USE sql_invoicing;
  715. DELIMITER $
  716. CREATE PROCEDURE get_clients()
  717. BEGIN
  718.         SELECT * FROM clients;

  719. END$

  720. DELIMITER ;
  721. ## CALL get_clients()

  722. -- >>>>>>> Creating Procedures Using MySQLWorkbench <<<<<<<

  723. -- >>>>>>> Dropping Stored Procedures <<<<<<<
  724. DROP PROCEDURE IF EXISTS get_clients_by_state
  725. ;


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 604个

财富等级: 财运亨通

发表于 2021-2-2 08:26:41 | 显示全部楼层
SQL本身就是一种语言了,还是比较复杂。要在lisp中使用,先得在VB中练习好,我的方法就是两个字:放弃。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 20个

财富等级: 恭喜发财

 楼主| 发表于 2021-2-2 08:51:32 | 显示全部楼层
/db_自贡黄明儒_ 发表于 2021-2-2 08:26
SQL本身就是一种语言了,还是比较复杂。要在lisp中使用,先得在VB中练习好,我的方法就是两个字:放弃。

一通百通,10分钟学会的查询语句,估计可以用到80%的场景
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 375个

财富等级: 日进斗金

发表于 2021-2-2 10:09:04 | 显示全部楼层
CAD结合数据库和Excel开发,是很多好的数据处理方式
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 33个

财富等级: 招财进宝

发表于 2023-3-29 14:22:22 | 显示全部楼层
结合SQL,程序的应用会更加广泛,也可以做一些大量数据类交互类的编程,很有意义。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 226个

财富等级: 日进斗金

发表于 2023-7-16 08:12:35 | 显示全部楼层
谢谢分享!!!!!!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-21 20:38 , Processed in 0.473512 second(s), 39 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表