Jump to content

[Sql] Making Basic Sql Statements


Recommended Posts

Hi!

 

Since all the SQL tutorials here seem to be very much outdated (non existent). I wanted to write a tutorial on how to write basic SQL statements. SQL is a VERY complex and nasty language, there's so much more to know than I can cover in a simple tutorial, but I will try my best.

 

I will start with a tutorial about selecting data, but if you guys want me to make more for updating, deleting, creating, subqueries, counting, summing or whatever, tell me. 

 

Let's say we own a website which has users and these users can all update their profile with a certain country. Our tables look like this:

 

user

id country_id name

1 1 Example Name1

2 1 Example Name2

3 2 Example Name3

4 2 Example Name4

5 NULL Example Name5

 

country

id name

1 Australia

2 Germany

3 China

 

The most basic select statement looks like this:

 



SELECT columnname, columnname, columnname FROM tablename;


  

Let’s say we want to select the name and the id of all users, we would write it like this:

 



SELECT id, name FROM user;


  

This will return the name and id of ALL the records. It will look like this:

id name

1 Example Name1

2 Example Name2

3 Example Name3

4 Example Name4

5 Example Name5

 

But what if we only want to know the name and id from the users who live in Australia? We first need to look in our country table and we see that Australia has id 1. Now we have to add a ‘WHERE’ statement to our SELECT. The most basic SELECT WHERE statement looks like this:

 



SELECT columnname, columname, columnname FROM tablename WHERE columname = 'value' OR columname = 'value' AND columnname = 'value';


  

As you can see, you can add ORs and ANDs to your statement, this works just like you would expect it to work. If you are a programmer, you can see OR as || and AND as &&.

 

Now let’s select our Australian users:



SELECT id, name FROM user WHERE country_id = 1;


  

This will return only Australian users. It will look like this:

id name

1 Example Name1

2 Example Name2

 

But what if we want to select id, name and country name? How do we do that? We can do that with a JOIN. There are a lot of different joins; INNER JOIN, OUTER JOIN, RIGHT JOIN, LEFT JOIN, RIGHT OUTER JOIN, etc.

 

This is a lot of information to take in at once, so we’ll keep it simple and narrow this guide down to two JOINs: the LEFT JOIN and the INNER JOIN. 

So what is the difference? I’ll show you this in the following examples, since writing it down is difficult.

 

The most basic JOIN looks like this:



SELECT tablename1.columnname , tablename1.columname, tablename2.columname FROM tablename1
JOIN tablename2 ON tablename2.keycolumn1 = tablename1.keycolumn2;


  

The key columns represent a column belonging to table2 which is also present in table1.

Now to ALSO select the country name for all users, we write our SELECT statement like this:

 



SELECT user.id, user.name, country.name FROM user
LEFT JOIN country ON country.id = user.country_id;


 

We can add some aliases to make our query a bit more readable:

 



SELECT u.id, u.name, c.name FROM user u
LEFT JOIN country c ON c.id = u.country_id;


 

Using aliases is NOT a must, it just comes down to a matter of preference whether you want to use it or not. 

 

However if you are selecting two columns with the same column name, in our example this is u.name and c.name, you have to add an alias to one of them. I would do it like this:

 



SELECT u.id, u.name, c.name country_name FROM user u
LEFT JOIN country c ON c.id = u.country_id;


  

This query will return the following:

id name country_name

1 Example Name1 Australia

2 Example Name2 Australia

3 Example Name3 Germany

4 Example Name4 Germany

5 Example Name5 NULL

 

But wait.. Example Name5 hasn’t got a country_id filled in, what if we only want users who do have a country_id? This is where the INNER JOIN comes in handy, it will remove all those NULL records from the query result.

 

We use the exact same query and only change LEFT JOIN to INNER JOIN, which will look like:



SELECT u.id, u.name, c.name country_name FROM user u
INNER JOIN country c ON c.id = u.country_id;


  

Now our result is:

id name country_name

1 Example Name1 Australia

2 Example Name2 Australia

3 Example Name3 Germany

4 Example Name4 Germany

 

Thanks.

 

PS: I dont own this guide, i copied it from a pdf file to share it with people keep in mind and thanks again.

Edited by Sawadee
  • Downvote 1
Link to comment
Share on other sites

Nice beginner guide! :)

Btw, you should use parentheses in WHERE columname = 'value' OR columname = 'value' AND columnname = 'value'...

Link to comment
Share on other sites

Nice beginner guide! :)

Btw, you should use parentheses in WHERE columname = 'value' OR columname = 'value' AND columnname = 'value'...

 

 

just made my exams from it so at start i can offer for free and see more in future

 

I will fix it thanks for the tip brother :) 

 

It could be nice to see people helpin each other on Lineage2 Section actually thats what this community needs.

Link to comment
Share on other sites

-1 reputation due to copy paste without proper credits. I was going to give you +1 but then elfo happened. 

Is fine if you copy paste, everyone does it. But at least give credits even if is 5 lines or 4 lines. 

 

If you sit down and make a better and bigger tutorial with more examples such as if i want to find ID of a monster that type is not equal to X 

i'll give you again +

Link to comment
Share on other sites

 

 

-1 reputation due to copy paste without proper credits. I was going to give you +1 but then elfo happened. 

Is fine if you copy paste, everyone does it. But at least give credits even if is 5 lines or 4 lines. 

 

If you sit down and make a better and bigger tutorial with more examples such as if i want to find ID of a monster that type is not equal to X 

i'll give you again +

 

I didnt copied anything from RageZone, and i simply wanted to share this guide from a tutorial guide.pdf that i have on my pc, So do i have to give credits to my Pdf? i dont care about reputations but explain me why do you butthurt some much and instead of doin actually something you just trashtalkin other topics and posts? and please dont start tellin me that This my opinion and im free to say anythin, if you wanna say anythin u want just go on spam topics or make a guide share it with people and dont expect anythin.

Edited by Sawadee
Link to comment
Share on other sites

I didnt copied anything from RageZone, and i simply wanted to share this guide from a tutorial guide.pdf that i have on my pc, So do i have to give credits to my Pdf? i dont care about reputations but explain me why do you butthurt some much and instead of doin actually something you just trashtalkin other topics and posts? and please dont start tellin me that This my opinion and im free to say anythin, if you wanna say anythin u want just go on spam topics or make a guide share it with people and dont expect anythin.

 

Lel, i made the biggest guide since 2012 about l2j gathered 100k views +

Also now you're being bitch so you deserve -1 and xdem's laugh. 

Link to comment
Share on other sites

He even c/p "Hi" just promote this autist already

 

Lel, i made the biggest guide since 2012 about l2j gathered 100k views +

Also now you're being bitch so you deserve -1 and xdem's laugh. 

 

I wont argue with people like you, once i did while u wasnt even registered on mxc. U are free to post anythin you want im not gonna feed some random trolls :) NM2YaH5.png

Edited by Sawadee
Link to comment
Share on other sites

i have on my pc, So do i have to give credits to my Pdf? 

Oh... and you 'PDF' is saying :

 

I will start with a tutorial about selecting data, but if you guys want me to make more for updating,

 

You want ur pdf make updates? :D

 Its cool bro :D

Edited by melron
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Posts

    • ready server for sale, also available for testing with ready and beautiful npc zone pvp with custom 2 epic core orfen lvl2 with all maps ready all quests work at 100% ready comm  board with buffer teleport gm shop service anyone interested send me a pm many more that I forget  Exp/Sp : x30 (Premium: x40)    Adena : x7 (Premium: x10)   Drop : x7 (Premium: 10)   Spoil : x7 (Premium: 10)   Seal Stones : x7 (Premium: 10)   Raid Boss EXP/SP : x10   Raid Boss Drop : x3 (Premium: x5)   Epic Boss Drop : x1 Enchants   Safe Enchant : +3   Max Enchant : +16   Normal Scroll of Enchant Chance : 55%   Blessed Scroll of Enchant Chance : 60% Game Features   GMShop (Max. B-Grade)   Mana Potions (1000 MP, 10 sec Cooldown)   NPC Buffer (Include all buffs, 2h duration)   Auto-learn skills (Except Divine Inspiration)   Global Gatekeeper   Skill Escape: 15 seconds or /unstuck   1st Class Transfer (Free)   2nd Class Transfer (Free)   3rd Class Transfer (700 halisha mark)   Subclass (Items required from Cabrio / Hallate / Kernon / Golkonda + Top B Weapon + 984 Cry B)   Subclass 5 Subclasses + Main (Previous subclasses to level 75 to add new one)   Noblesse (Full Retail Quest)   Buff Slots: 24 (28 with Divine Inspiration LVL 4)   Skill Sweeper Festival added (Scavenger level 36)   Skill Block Buff added   Maximum delevel to keep Skills: 10 Levels   Shift + Click to see Droplist   Global Shout & Trade Chat   Retail Geodata and Pathnodes   Seven Signs Retail   Merchant and Blacksmith of Mammon at towns   Dimensional Rift (Min. 3 people in party to enter - Instance)   Tyrannosaurus drop Top LS with fixed 50% chance   Fast Augmentation System (Using Life Stones from Inventory)   Chance of getting skills (Normal 1%, Mid 3%, High 5%, Top 10%)   Wedding System with 30 seconds teleport to husband/wife Olympiad & Siege   Olympiad circle 14 days. (Maximum Enchant +6)   Olympiads time 18:00 - 00:00 (GMT +3)   Non-class 5 minimum participants to begin   Class based disabled   Siege every week.   To gain the reward you need to keep the Castle 2 times. Clans, Alliances & Limits   Max Clients/PC: 2   Max Clan Members: 36   Alliances allowed (Max 1 Clans)   24H Clan Penalties   Alliance penalty reset at daily restart (3-5 AM)   To bid for a Clan Hall required Clan Level 6 Quests x3   Alliance with the Ketra Orcs   Alliance with the Varka Silenos   War with Ketra Orcs   War with the Varka Silenos   The Finest Food   A Powerful Primeval Creature   Legacy of Insolence   Exploration of Giants Cave Part 1   Exploration of Giants Cave Part 2   Seekers of the Holy Grail   Guardians of the Holy Grail   Hunt of the Golden Ram Mercenary Force   The Zero Hour   Delicious Top Choice Meat   Heart in Search of Power   Rise and Fall of the Elroki Tribe   Yoke of the Past     Renegade Boss (Monday to Friday 20:00)   All Raid Boss 18+1 hours random respawn   Core (Jewel +1 STR +1 DEX) Monday, Wednesday and Friday 20:00 - 21:00 (Maximum level allowed to enter Cruma Tower: 80)   Orfen (Jewel +1 INT +1 WIT) Monday to Friday, 20:00 - 21:00 (Maximum level allowed to enter Sea of Spores: 80)   Ant Queen Monday and Friday 21:00 - 22:00 (Maximum level allowed to enter Ant Nest: 80)   Zaken Monday,Wednesday,Friday 22:00 - 23:00 (Maximum level allowed to enter Devil's Isle: 80)   Frintezza Tuesday, Thursday and Sunday 22:00 – 23:00 (Need CC of 4 party and 7 people in each party min to join the lair, max is 8 party of 9 people each)   Baium (lvl80) Saturday 22:00 – 23:00   Antharas Every 2 Saturdays 22:00 - 23:00 Every 2 Sundays (alternating with Valakas) 22:00 – 23:00   Valakas Every 2 Saturdays 22:00 - 23:00 Every 2 Sundays (alternating with Antharas) 22:00 – 23:00   Subclass Raids (Cabrio, Kernon, Hallate, Golkonda) 18hours + 1 random   Noblesse Raid (Barakiel) 6 hours + 15min random   Varka’s Hero Shadith 8 hours + 30 mins random (4th lvl of alliance with Ketra)   Ketra’s Hero Hekaton 8 hours + 30 mins random (4th lvl of alliance with Varka)   Varka’s Commander Mos 8 hours + 30 mins random (5th lvl of alliance with Ketra)   Ketra’s Commander Tayr 8 hours + 30 mins random (5th lvl of alliance with Varka)
    • Have a great day! Unfortunately, we can not give you the codes at the moment, but they will be distributed as soon as trial is back online, thanks for understanding! Other users also can reply there for codes, we will send them out some time after.
    • Ok mates i would like to play a pridestyle server (interluide, gracie w/ever) Is there any such server online and worth playing?
    • L2 BATTLE OF RAGNAROK - DISCORD LOGO Battle of Ragnarok - Discord      
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock