You are reading the article How Regex Works In Kotlin With Examples? updated in December 2023 on the website Cattuongwedding.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested January 2024 How Regex Works In Kotlin With Examples?
Introduction to Kotlin RegexWeb development, programming languages, Software testing & others
Syntax of Kotlin RegexIn kotlin language the regular expression is mainly used to find and search the text in the html contents and it provides the Regex() class to deal with the other default functions. It has many operators, operands, and other symbols to perform the operations in the kotlin application.
{ val vars1=”values”.toRegex() val vars2=”values1″ val res=regex.replace(vars2,”new”) val match=regex.matches(“values”)?.value —-some logic codes depends on the requirement— }
The above codes are the basic syntax for to utilizing the regular expression in the specified strings with the operands and operators like special characters and symbols.
How does Regex work in Kotlin? Examples of Kotlin RegexGiven below are the examples mentioned:
Example #1Code:
import java.util.Scanner; fun firstExample(num1: Int, str2: String) { println("Welcome User Integer is the first input and string type is the second input.") println("$it.") } } fun main() { val ptn = Regex("^b") println(ptn.containsMatchIn("abc")) println(ptn.containsMatchIn("bac")) println("Welcome To My Domain its the first example related to the kotlin regex expression") println("#############################################################") val q = arrayOf(8,2,4) q.forEach { println(it*it) } var s1 = Scanner(System.`in`) print("Please enter your Jewellery Shop:") var jshop = s1.nextInt() var out = when(jshop){ println("Your brand is not listed here") } } println(out) inp1["Model Name"] = "Dell Inspiron" inp1["Model Price"] = "35000" inp1["Model ID"] = "45632" for ((k, v) in inp1) { println("Thank you users your inputs are $k = $v") } val inp2 = mapOf("Model Name" to "HP", "Model Price" to 54000, "Model ID" to 89765) println("Your input Keys are:" + inp2.keys) println("Your input Values are:" + inp2.values) println("Thank you users have a nice day please keep and spent time with our application") firstExample(41, "Siva") firstExample(44, "Raman") }Output:
In the above example, we used the collection concept to perform the data operations with included the regular expression.
Example #2Code:
enum class pc(val x: Boolean = true){ Sony, Dell, Lenovo, Apple, Wipro, HCL; companion object{ fun pcdetails(obj: pc): Boolean { } } } fun compdetails(comp: pc) { when(comp) { } } fun main() { val urptn = "[a-zA-Z0-9]+" val fr = Regex(urptn) println(fr.matches("Siva_Raman")) println(fr matches "Siva_Raman") println(fr.matches("Siva_Raman")) val inp1 = Regex("ab.") res.forEach() { } println(res) var ptn = Regex("Welcome To My Domain its the second example that related to the kotlin regex expression?") println(ptn.matchEntire("Welcome To My Domain its the second example that related to the kotlin regex expression")?.value) println(ptn.matchEntire("Users Have a Nice Day")?.value) ptn = Regex("""D+""") println(ptn.matchEntire("Welcome To My Domain its the second example that related to the kotlin regex expression")?.value) println(ptn.matchEntire("Please spent the time with us")?.value) val uptn = "[a-zA-Z0-9]+" val sec = Regex(uptn) println(sec.containsMatchIn("Siva_Raman is the SOftware Support Engineer and it works in XYZ Company")) println(sec.containsMatchIn("!#$%")) }Output:
Example #3Code:
fun main() { val inp1 = listOf("Tamil", "English", "Physics", "Chemistry","Biology", "ComputerScience", "Engineering Graphics") val ptn = "Tamil".toRegex() println("*********************") println("Welcome To My Domain its the Third Example that related to the Kotlin RegEx expression") println("We used the other default regular expression method like ContainsMatchIn function") if (ptn.containsMatchIn(ls)) { println("$ls matches") } } println("*********************") println("Like that we used the Regular Expression matches function") if (ptn.matches(ls)) { println("Your input datas are matched and see below results : $ls matches") } } }Output:
In the final example, we used other pre-defined regular expression methods like matches and patterns to perform the user input operations.
ConclusionIn kotlin language, we used a lot of default methods, keywords, and variables to perform the operations in an application. Like that the application task varies depends upon the requirement among that regular expression is to find the string values and matches or compare with the same path or different path of the variable memory location.
Recommended ArticlesThis is a guide to Kotlin Regex. Here we discuss the introduction, syntax, and working of regex in Kotlin along with different examples for better understanding. You may also have a look at the following articles to learn more –
You're reading How Regex Works In Kotlin With Examples?
How Map Function Works In Kotlin With Examples?
Introduction to Kotlin Map
Web development, programming languages, Software testing & others
SyntaxThe kotlin language used many features as similar to java like collection, util package and even other packages classes and its method behaviours are the same as java. A map is one of the interfaces that can hold the datas as key-value pairs like java.
{ —–some coding logics it depends upon the application requirement— }
The above codes are the basic syntax for utilising the map interface on the kotlin. The mapping interface uses any type like Integer, String, Characters as the parameter type.
How does Map Function work in Kotlin?A map is an interface that is used for to store the datas as the key-value pairs. Like java, it can be of the same type and feature. It’s worked in the kotlin. The key is the unique one, and it holds only a single value for each key; it also different pairs and combined with each other. It is the immutable one, and it is the fixed size, so after declaring the mapping size, it cannot be changed both static and dynamic. The members from the map interface are readable ones, so we cant edit and changeable for that. If we use the mutablemap interface, it supports both read and write access.
The key type is the invariant one, and it is accepted as the parameter; it uses a method like containsKey() and also it returns the keys set. Like that map value also covariant and its value type it also be changed if it’s required. The classes used on the map interface will be of any type; this is because of the classes used internally on the kotlin package. The map is of any type in the kotlin language; the key and value elements will be stored as a separate index; it takes the predicate arguments, the filter conditions will be of the negative type, and the key will be unique and it has many values.
Examples of Kotlin MapGiven below are the examples of Kotlin Map:
Example #1Code:
package one; { val map = mapOf(1 to "Siva", 2 to "raman" , 3 to "Welcome", 4 to "To", 5 to "My Domain", 12 to "Its the twelth index and key value", 13 to "Its the thirteen index and key value", 14 to "Its the fourteen index and key value", 15 to "Its the fifteen index and key value") println( map) println("Its the first example for reagrding the kotlin map interface we used different classes and methods that can be related to the interface") val map1 = mapOf(6 to "Have a Nice Day users", 7 to "Its the Seventh index and key value" , 8 to "Its the eigth index and its key value", 9 to "Its the ninth index and its key value", 10 to "Its the tenth index and its key value", 11 to "Its the eleventh index and its key value") println("Thank you users for to spenting the time with our application your entries are : "+map1) println("Its the keys entries and it is used to store the variable : "+map1.keys ) println("Its the values the entries are used to store as the separate variable "+map1.values ) }Output:
In the above example, we used a map interface, and it will store the user inputs as key-value pairs. We can use mapOf() method for to set the key values and print them accordingly.
Example #2Code:
package one; { val first = mapOf("siva" to 1, 1 to "raman", 'A' to "Welcome To My Domain", "Have a Nice Day Users" to 'A') for ((k,v) in first) { println("Your input key is: $k and your input value is $v") } println("Your input key is: $k and your input value is $v") } println("Your input map size is: ${third.size}") println("Your input entries are: ${third.entries}") println("Your input key is: ${third.keys}") println("Your input values are: ${third.values}") println("Your Employeed ID is: ${third.get("Employee ID")}") println("Your Serial No is: ${third["SNO"]}") println("Your proofs are: ${third.getValue("ProofID")}") println("Your Employeed ID contains: ${third.containsKey("Employee ID")}") println("Your Serial No contains: ${third.contains("SNO")}") println("Your ProofID contains: ${"Chemistry" in third}") println("If any of the columns or attributes missing please add it separately: ${third.containsValue(1234566)}") println("The Additional or Missing attribute inputs are: ${third.getOrDefault("pincode",600063)}") }Output:
Example #3Code:
package one; { val third = mutableMapOf("Employee ID" to 1, 2 to "Employee Name", 3 to "Address", 4 to "City", 5 to "pincode") println(third) println(four) five.put("zipcode", 27346) println(five) five["SNo"] = 1234 println(five) five.putIfAbsent("landline", 91) five.putIfAbsent("MobileNumber", 2436) println(five) five.replace("Employee ID", 1) println(five) five.replace("Employee Name", 2, 3) println(five) five.remove("Address") println(five) five.remove("City", 4) println(five) five.clear() println(five) }Output:
In the final example, we used a mutable map interface for to store the user inputs on key-value pairs. We used additional methods like remove(), replace(), clear() etc for to perform the operations.
ConclusionIn kotlin, we used a lot of collection util packages for to store, extract and retrieve the datas. Based on the requirements, we used specific util packages like map, list in a map we used a different set of interfaces and its methods for to implement the application which depends on the user requirements.
Recommended ArticlesThis is a guide to Kotlin Map. Here we discuss the introduction, syntax, and working of the map function in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –
List Of Operators In Kotlin With Their Examples
Introduction to Kotlin Operators
The following article outlines the most commonly used operators in Kotlin. Operators are special symbols used to perform specific operations on the operands. For example, the (-) operator performs a subtraction between two operands.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
67-7=60
Operators in Kotlin, Here 67 and 7 are operands, and – is an operator.
Types of Kotlin OperatorsGiven below are the different types:
1. Arithmetic OperatorsArithmetic Operators perform basic arithmetic calculations like subtraction, addition, multiplication, division, etc. In Kotlin, simple a + b is interpreted as a.plus(b) as a function call.
Operator Meaning Example
+ Add two operands a + b
– Subtract the right operand from the left operand a – b
* Multiply two operands a * b
/ Divide the left operand by the right one a / b
% Modulus – returns remainder on dividing two numbers a % b (deprecated from v 1.1)
Example:
package com.examples { var num1 = 64 var num2 = 32 val answer : double answer = num1 +num2 println ("sum = $answer") answer = num1 - num2 println ("diff = $answer") answer =num1 * num2 println ("mult = $answer") answer = num1 / num2 println ("div = $answer") answer = num1 % num2 println ("mod = $answer") }Note: The + operator is also used for String concatenation with strings.
Example:
package com.examples { val fname = "Laxman" val lname = "das" val full_name = fname + " " + lname println (full_name) } 2. Assignment OperatorsAssignment Operator is used to assign values to variables. The value after evaluation on the left-hand side is assigned to the right-hand side variable.
Besides the basic = assignment operator, Kotlin provides a wide range of assignment Operators, which are mentioned below:
Operator Example Equivalent to
+ = a+=b a=a+b
-= a-=b a=a-b
*= a*=b a=a*b
/= a/=b a=a/b
%= a%=b a=a%b
Example:
package com.examples { var number1 = 22 var number2 = 20 number1 + = 10 number2 % = 3 println ("Result1 = $number1") } 3. Unary OperatorsUnary Operators are those that work only on a single operand. Increment ( ++ ) and Decrement ( — ) operators are shortcuts of x = x+1 and x = x – 1
Operators Example Equivalent to
+ +a + (a value)
– -a – (a value)
! !a Not a (inversion)
++ ++a a=a+1
— –a a=a-1
Example:
package com.examples { var a = 5 var b = 10 var c = 15 negation = -a increment = ++b dec = c-- println ("Negation = $negation") }In the above example, the value of b is first incremented by 1 and then assigned to variable’ increment,’ whereas the value of c is first assigned to variable dec and then decreases by 1.
4. Comparison OperatorsComparison Operators are used for comparing the two values. These operators are mostly used with if-else for executing specific flow after comparison.
Operator Meaning Expression
> Greater than
< Less than a < b
>= Greater than equals to
<= Less than equals to a <= b
Example:
{ var num1 = 20 var num2 = 30 { println (“greater number is $num1”) } else { println (“greater number is $num2”) } }
5. Equality and Non-equality OperatorsKotlin provides a set of logical operators, and these equality and non-equality operators return a boolean value depending on the comparison result. These operators play an important role in the program logic flow by comparing the values and moving the flow according to that.
Operators Meaning Expression
!= Not equal to a != b
== It is equal to a ==b
Example:
{ var a = 3 var b = 6 println (a==b) }
6. Logical OperatorsKotlin provides the below-mentioned 3 logical operators, which return boolean values, either true or false.
Operators Meaning Expression
&& True if all the values are true a && b (meaning a and b)
|| True if any of the value is true
! True if the value is false! a (meaning not a )
Example:
{ var a = 20 var b = 4 var c = -8 val answer : Boolean }
7. In operatorOperators Meaning Expression
in Is a present in collection b a in b
Not in Is a not present in Collection b a !in b
Example:
{ val array = intArrayOf(10, 20, 30 ,40) If (20 in array) { println (“yes 20 is present in array”) } else { println (“no 20 is not present in array”) } }
8. Range OperatorKotlin uses the range operator to create a range of values. This operator is handy when working with loops. It is unnecessary to define every value if it is sequential; it is better to use a shortcut and define the range specifying the lowest and highest value.
Operator Meaning Expression
. . If i is in the range from a to b For (i in a . .b)
Example:
{ for (i in 1..10) { println (“value of i is $i”) } }
9. Indexed Access OperatorIndexed Access operators access any value at a particular index in an array. In Kotlin array starts with an index of 0.
Operators Meaning
a [i] Get the value at index’ i’ in an array ‘a’
a [i] = b Set the value b at the ‘i’ index of an array ‘a’
Example:
{ val array = intArrayOf(10, 20, 30, 40, 50) var value = array[1] println(“value at index 1 is $value”) array[1] = 90 println (“recent value at index 1 is $array[1]”) }
10. Bitwise OperatorsLike other programming languages, e.g., C, C++, and Java, Kotlin has no bitwise operators. It has various functions that work for bitwise operations.
Functions Meaning
shl Signed shift left
shr Signed shift right
ushr Unsigned shift right
and Bitwise and
or Bitwise or
xor Bitwise xor
inv Bitwise Inversion
Example:
{ var a = 12 var b = 10 val result1 : Int val result2 : Int result1 = a and b result2 = a or b println (“final result of and operation is $result1”) Println (“final result of or operation is $result2”) // final result of or operation is 14 }
Recommended ArticlesThis has been a guide to Kotlin Operators. Here we discuss the introduction, different operators used in Kotlin, and their examples. You can also go through our other suggested articles to learn more –
How Xpath Attribute Works With Examples?
Definition of XPath attribute
For finding an XPath node in an XML document, use the XPath Attribute expression location path. We can use XPath to generate attribute expressions to locate nodes in an XML document. When there are certain uniquely recognized attribute values accessible in the container tag, we employ the XPath functions (contains or starts-with) with the attribute. The “@” symbol is used to access attributes.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
Xpath Syntax is given as:
Within a single node
/path to/element[@attribute_name]
At any point in the document
//*[@attribute_name]
Within a node that has some value
/path to/element[@attribute_name='search value'] /path to/element[@attribute_name="search value"]
//*[@attribute_name='search string'] How XPath attribute works?
Attribute predicates are another approach to indicate the items we want to choose more accurately. They are divided into two types: [@attr=”value”] chooses only elements with the attr attribute set to a value, and [@attr] selects only elements with an attr attribute set to a value (set to any value).
For example, let’s see an XML code like:
/purchase/product[@id=”soap”]/name
The following examples, which deal with qualities, are a little more complicated.
1. Choose objects that begin with the word “imagebox”:
[starts-with(name(),’imagebox’)]
2.Only objects from the “gallery” set should be selected:
/*[@sets=',gallery,']
3.Select the following objects from the set “gallery” that begins with the name albums:
/*[starts-with(name(),'albums')] /*[@sets=',gallery,']
4.Choose between img_1 and img_ 2:
5.Select all objects in the “blue” or “flower” sets:
//*[@sets,',blue,') or contains(@sets,'flower')]
6.Select all objects in the “blue” set but not the “flower” set:
//*[contains(@sets,',blue,') and not(contains(@sets,'flower')]
7.Select all items in sets that start with the letter “pot”:
//*[starts-with(@sets,',pot')]
Following Observations could be taken to see how attribute works with Xpath functions:
– With absolute text or an attribute, we can also utilise the contains () and starts-with() methods.
Let’s take a real-time case:
When using the contains() and starts-with() methods, we need to be very careful about which attribute we utilize. We won’t be able to uniquely identify the element if the property value isn’t unique. The XPath won’t function if we use the “type” attribute to identify the “I’m Feeling Lucky” button.
The presence of two matching nodes indicates that the element has not been appropriately detected. The value of the type attribute is not unique in this case.
For instance
The attribute expression is
//chi/@name
The result would be
In the following section, we shall see how attribute works in java Programming to extract the specific element from the XML document rather than calling all the elements. Let’s get started:
ExamplesLet us discuss examples of the XPath attribute.
Example #1: Using Simple Xpath Expressionchúng tôi
/stateinfo/client/@id
Output:
ii) Selecting all attribute value
This selects all attributes of the specified context node.
Output:
iii) Selecting discipline attribute
@discipline
Output:
Example #2.xml
/Universe/*[@uname]
Explanation
With Xpath expression let’s see how to extract the value of an attribute.
Output:
/Universe/*[@uname='Star']
Output:
Find nodes by substring matching the value of an attribute.
Xpath Expression
/Universe/*[contains(@uname,'Ma')]
Output:
Find nodes using a substring that matches the beginning of an attribute’s value.
/Universe/*[starts-with(lower-case(@uname),'sta')]
Output:
Example #3: Using java}
chúng tôi
Explanation
Output:
ConclusionTherefore, this article offers a code example that shows how to implement this requirement by using the contains XML Path Language (XPath) Attribute function. In the following post, we’ll look at how to use XPath Functions to improve the position of elements on the available webpage.
Recommended ArticlesThis is a guide to XPath attribute. Here we discuss the definition, How XPath attribute works? And examples for better understanding. You may also have a look at the following articles to learn more –
How Haskell Stack Works With Examples?
Definition of Haskell Stack
In Haskell we have stack data structure which is also used to store the element inside it. Also to use the stack we have to include libraries into the application it is not present in Haskell. In this section, we will use Data. Stack library, also it provide us so many functions which is used to perform different operations in element present inside it. In Stack data structure we add and delete the element at the top of the stack only, it works in the last in first out manner. In the coming section of the tutorial, we will see the internal working of the stack in detail which will help us to get a clear idea of how to use this while programming in Haskell and also its implementation in detail to get start with.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
stackNew:: Stack aAs you can see in the above line of syntax we are creating a simple stack by using the keyword ‘stack’ but to implement this we have to use the Data. Stack library into our code. Let’s take a sample piece of syntax for better understanding see below;
e.g. :
As you can see in the above line of syntax we are trying to use one of the methods from the Data. Stack library in Haskell. Without this library, it will not work in code. In the coming section of the tutorial, we will see the internal working of the stack in detail and how it functions internally with different types of functions available inside the Data. Stack library.
How Haskell stack works?As we already know the stack is a data structure available in Haskell which is used to store the elements inside it. This stack works in the same way like any other programming language, but it mainly has two operations which are very important to understand the stack working in detail. In this section, we will see the flow of the stack in Haskell in detail also with the flow chart diagram with all the steps in detail for beginners to understand it better. First, take a look at the flow chart see below;
1) Stack works in the LIFO manner which means last in first out order. This approach suggests or indicted that the element which has entered first will be the last to come out or vice versa in general.
2) In Stack we always remove or add the elements from the one end only. that means this satisfies the LIFO completely here.
3) In the stack we generally use the push and pop method to add r delete the element from the stack.
4) If we have used to push method that ends it will add a new element to the stack.
5) If we use the pop method then it will remove the first element or the available first element from the given stack.
6) After all the operations being performed it will exit.
Now we will see some of the methods which are available inside the Data. Stack package which we can use to perform some operations on the stack, let’s take a closer look for better understanding see below;
1) stackPush: This is the function available inside the Data. Stack we have to have this package into the code.
e.g. :
As you can see it is the official documentation of the stack push declaration given by Haskell.
2) stackNew: This is the function available inside the Data. Stack we have to have this package into the code. Basically, this function is used to create an empty stack. Let’s take a closer look at the syntax for this function given by the Haskell documentation see below;
e.g. :
stack new :: Stack aAs you can see it is the official documentation of the stack push declaration given by Haskell.
e.g. :
As you can see it is the official documentation of the stack push declaration given by Haskell.
4) stackIsEmpty: This is the function available inside the Data. Stack we have to have this package into the code.
e.g. :
As you can see it is the official documentation of stackPush declaration given by Haskell.
5) stackSize: This is the function available inside the Data. Stack we have to have this package into the code. Basically, this function is used to check the size of the stack how many elements present inside it. Let’s take a closer look at the syntax for this function given by the Haskell documentation see below;
e.g. :
As you can see it is the official documentation of stackPush declaration given by Haskell.
Now we can see one example of how it works in actuality see below;
stack = [2, 3, 4, 5 ]Suppose we have this stack created with some numbers inserted inside it. So let’s suppose we have inserted 2 then it will be the last to remove because it works in the LIFO manner. after that, we have inserted 3 then if we want to remove them it will remove 3 first then the first inserted element.
ConclusionBy the use of stack, we can store our elements inside it, also easy to use and handle in Haskell. But it is not presented in Haskell directly we have to include the Data. Stack library for this to work otherwise it will not work. These are very easy to use and handle also readable by the developers as well.
Recommended ArticlesWe hope that this EDUCBA information on “Haskell Stack” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
How With Clause Works In Mysql?
Introduction to MySQL WITH Clause
MySQL WITH clause defines the CTE (Common table expressions). A common table expression is a named temporary result set that can be used multiple times. The CTE can be defined using WITH clause and can have one or more sub-clauses separated by a comma. The defined sub-clauses consist of each result set associated with a name to it. CTE can be used in other CTEs. We also have recursive CTE, which will be referred to as itself. The result set exists only within the scope of a single statement as SELECT, INSERT, UPDATE, or DELETE.
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
In this session, let us see how CTE will be defined using the WITH clause, along with an example.
Syntax of MySQL WITHNow let us consider the syntax of declaring the CTE using WITH clause:
1. Declaration of Single CTE< QUERY STATEMENT >
2. Declaration of Multiple CTESyntax:
< QUERY STATEMENT > < QUERY STATEMENT >
WITH clause can be used in the beginning as:
WITH … SELECT …
WITH … UPDATE …
WITH … DELETE …
WITH statement can immediately precede as below:
CREATE TABLE … WITH … SELECT …
CREATE VIEW … WITH … SELECT …
INSERT … WITH … SELECT …
REPLACE … WITH … SELECT …
DECLARE CURSOR … WITH … SELECT …
EXPLAIN … WITH … SELECT …
How does WITH Clause Work in MySQL?Now let us consider the tables that apply the CTE concept using the WITH clause:
1. Let us create a Table, use the above Syntax format, and insert Data into the Table.
create table Order_detail ( shop_id int, product_id int, brand_id int, Shop_name varchar(20), quantity int, price int );2. Now, let us insert Data into the Table.
insert into Order_detail (shop_id, product_id, brand_id, Shop_name, quantity, price) values (1,1,1,'Kellogs',45,45); insert into Order_detail (shop_id, product_id, brand_id, Shop_name, quantity, price) values (2,7,5,'Fantasy Store',75,145); insert into Order_detail (shop_id, product_id, brand_id, Shop_name, quantity, price) values (3,3,2,'Laxshmi store',65,85); insert into Order_detail (shop_id, product_id, brand_id, Shop_name, quantity, price) values (4,4,7,'General store',25,65); insert into Order_detail (shop_id, product_id, brand_id, Shop_name, quantity, price) values (5,5,8,'Corn store',35,75);Query:
select * from Order_detail;Output:
3. Now let us Perform the Simple CTE Creation on the above Table.
Query:
With CTE_Orders_Store AS ( Select shop_id, product_id, brand_id, shop_name, quantity from order_detail where UPPER(shop_name) like '%store%' )Output:
Explanation: Here we are finding the shops that have stored in their “shop_name” and the number of products sold exceeds 50.
4. Now let us Perform another Simple CTE on the above Table.
Query:
With CTE_Orders_quantity AS ( Select shop_id, product_id, brand_id, shop_name, quantity ) Select * from CTE_Orders_quantity where shop_idin( 2, 1);5. Now let us Consider the above-Created table “order_detail” and create another Table as below.
Query:
create table Details_People ( id int, name varchar(20), location varchar(20), pincodeint, product_idint );6. Insert the Below Rows into the Table as below.
Query:
insert into DETAILS_PEOPLE values (1, 'Sam', 'MP', 564321,1); insert into DETAILS_PEOPLE values (2, 'Sohan', 'Bangalore', 523321,7); insert into DETAILS_PEOPLE values (3, 'Will', 'Tamilnadu', 523021,3); insert into DETAILS_PEOPLE values (4, 'Ben', 'UP', 564000,3); insert into DETAILS_PEOPLE values (5, 'Hamington', 'UP', 564000,4); insert into DETAILS_PEOPLE values (6, 'Ji eun', 'Bangalore', 523321,2); insert into DETAILS_PEOPLE values (7, 'Jimin', 'UP', 564000,5); insert into DETAILS_PEOPLE values (8, 'Jk', 'Bangalore', 523321,4); insert into DETAILS_PEOPLE values (9, 'V', 'AP', 590001,5); insert into DETAILS_PEOPLE values (10, 'Jhope', 'Bangalore', 523321,1);7. Now let us Select the Columns from the Table
Query:
Select * from Details_People;Output:
8. let us perform the Complex CTE Between the two Tables.
With CTE_Orders_Store AS ( Select shop_id, product_id, brand_id, shop_name, quantity from order_detail where UPPER(shop_name) like '%store%' ) Select * from CTE_Orders_Stores JOIN details_people p ON s.product_id=p.product_idOutput:
Explanation: Here in the above query, we are finding the shops which have stored in their “shop_name” and the number of products sold greater than 50. The output of the CTE we have joined with the “details_people” and got the output.
Query:
With CTE_Orders_quantity AS ( Select shop_id, product_id, brand_id, shop_name, quantity ) Select * from CTE_Orders_quantity s JOIN details_people p ON s.product_id=p.product_id where shop_idin( 2, 1);Output:
9. Now let us see the RECURSIVE CTE
Query:
WITH RECURSIVE REC_cte AS ( SELECT 1 AS A, 1 AS B, -1 AS C UNION ALL SELECT A + 1, B * 2, C * 2 FROM REC_cte WHERE A < 5 ) SELECT * FROM REC_cte;Output:
Conclusion
MySQL WITH clause defines the CTE (Common table expressions). A common table expression is a named temporary result set that can be used multiple times.
You can define the CTE using the WITH clause, which allows for one or more sub-clauses separated by a comma in active voice.
Each sub-clause in the CTE defines a result set and associates it with a name. You can use CTEs within other CTEs. Additionally, recursive CTEs refer to themselves.
The result set exists only within the scope of the single statement as SELECT, INSERT, UPDATE, or DELETE
Recommended ArticlesWe hope that this EDUCBA information on “MySQL WITH” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
Update the detailed information about How Regex Works In Kotlin With Examples? on the Cattuongwedding.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!