right join versus left join - mysql

In this case, a left join is the same as a right join?
mysql>
mysql>
mysql> use usenet;show tables;describe ARTICLE;describe NEWSGROUP;
Database changed
+------------------+
| Tables_in_usenet |
+------------------+
| ARTICLE |
| NEWSGROUP |
+------------------+
2 rows in set (0.00 sec)
+---------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------+------+-----+---------+----------------+
| ID | bigint(20) | NO | PRI | NULL | auto_increment |
| MESSAGENUMBER | int(11) | YES | | NULL | |
| NEWSGROUP_ID | bigint(20) | YES | MUL | NULL | |
+---------------+------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| ID | bigint(20) | NO | PRI | NULL | auto_increment |
| NEWSGROUP | varchar(255) | YES | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql>
mysql> select * from ARTICLE right join NEWSGROUP on ARTICLE.NEWSGROUP_ID=NEWSGROUP.ID;
+------+---------------+--------------+----+-------------------------------+
| ID | MESSAGENUMBER | NEWSGROUP_ID | ID | NEWSGROUP |
+------+---------------+--------------+----+-------------------------------+
| 1 | 4 | 1 | 1 | gwene.com.androidcentral |
| 2 | 5 | 1 | 1 | gwene.com.androidcentral |
| 3 | 6 | 1 | 1 | gwene.com.androidcentral |
| 4 | 7 | 1 | 1 | gwene.com.androidcentral |
| 5 | 8 | 1 | 1 | gwene.com.androidcentral |
| 6 | 9 | 1 | 1 | gwene.com.androidcentral |
| 7 | 10 | 1 | 1 | gwene.com.androidcentral |
| 8 | 11 | 1 | 1 | gwene.com.androidcentral |
| 9 | 4 | 2 | 2 | gwene.com.blogspot.emacsworld |
| 10 | 4 | 3 | 3 | gwene.com.blogspot.googlecode |
| 11 | 5 | 3 | 3 | gwene.com.blogspot.googlecode |
| 12 | 6 | 3 | 3 | gwene.com.blogspot.googlecode |
| 13 | 7 | 3 | 3 | gwene.com.blogspot.googlecode |
| 14 | 8 | 3 | 3 | gwene.com.blogspot.googlecode |
| 15 | 9 | 3 | 3 | gwene.com.blogspot.googlecode |
| 16 | 10 | 3 | 3 | gwene.com.blogspot.googlecode |
| 17 | 11 | 3 | 3 | gwene.com.blogspot.googlecode |
| 18 | 4 | 4 | 4 | gwene.com.economist |
| 19 | 5 | 4 | 4 | gwene.com.economist |
| 20 | 6 | 4 | 4 | gwene.com.economist |
| 21 | 7 | 4 | 4 | gwene.com.economist |
| 22 | 8 | 4 | 4 | gwene.com.economist |
| 23 | 9 | 4 | 4 | gwene.com.economist |
| 24 | 10 | 4 | 4 | gwene.com.economist |
| 25 | 11 | 4 | 4 | gwene.com.economist |
+------+---------------+--------------+----+-------------------------------+
25 rows in set (0.00 sec)
mysql>
mysql> select * from ARTICLE left join NEWSGROUP on ARTICLE.NEWSGROUP_ID=NEWSGROUP.ID;
+----+---------------+--------------+------+-------------------------------+
| ID | MESSAGENUMBER | NEWSGROUP_ID | ID | NEWSGROUP |
+----+---------------+--------------+------+-------------------------------+
| 1 | 4 | 1 | 1 | gwene.com.androidcentral |
| 2 | 5 | 1 | 1 | gwene.com.androidcentral |
| 3 | 6 | 1 | 1 | gwene.com.androidcentral |
| 4 | 7 | 1 | 1 | gwene.com.androidcentral |
| 5 | 8 | 1 | 1 | gwene.com.androidcentral |
| 6 | 9 | 1 | 1 | gwene.com.androidcentral |
| 7 | 10 | 1 | 1 | gwene.com.androidcentral |
| 8 | 11 | 1 | 1 | gwene.com.androidcentral |
| 9 | 4 | 2 | 2 | gwene.com.blogspot.emacsworld |
| 10 | 4 | 3 | 3 | gwene.com.blogspot.googlecode |
| 11 | 5 | 3 | 3 | gwene.com.blogspot.googlecode |
| 12 | 6 | 3 | 3 | gwene.com.blogspot.googlecode |
| 13 | 7 | 3 | 3 | gwene.com.blogspot.googlecode |
| 14 | 8 | 3 | 3 | gwene.com.blogspot.googlecode |
| 15 | 9 | 3 | 3 | gwene.com.blogspot.googlecode |
| 16 | 10 | 3 | 3 | gwene.com.blogspot.googlecode |
| 17 | 11 | 3 | 3 | gwene.com.blogspot.googlecode |
| 18 | 4 | 4 | 4 | gwene.com.economist |
| 19 | 5 | 4 | 4 | gwene.com.economist |
| 20 | 6 | 4 | 4 | gwene.com.economist |
| 21 | 7 | 4 | 4 | gwene.com.economist |
| 22 | 8 | 4 | 4 | gwene.com.economist |
| 23 | 9 | 4 | 4 | gwene.com.economist |
| 24 | 10 | 4 | 4 | gwene.com.economist |
| 25 | 11 | 4 | 4 | gwene.com.economist |
+----+---------------+--------------+------+-------------------------------+
25 rows in set (0.00 sec)
mysql>
mysql>

Not really, because RIGHT JOIN and LEFT JOIN are symmetric. That is:
A LEFT JOIN B = B RIGHT JOIN A
RIGHT JOIN is merely syntactic sugar. The optimizer can rewrite a right join to a left join:
mysql> explain extended select * from t right join t t2 using (c1)\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: t2
type: index
possible_keys: NULL
key: c2
key_len: 5
ref: NULL
rows: 4201
filtered: 100.00
Extra: Using index
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: t
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: test.t2.c1
rows: 1
filtered: 100.00
Extra:
2 rows in set, 1 warning (0.00 sec)
Note the LEFT JOIN in the optimizer re-write (the tables are swapped):
mysql> show warnings\G
*************************** 1. row ***************************
Level: Note
Code: 1003
Message: select `test`.`t2`.`c1` AS `c1`,`test`.`t2`.`c2` AS `c2`,`test`.`t`.`c2` AS
`c2` from `test`.`t` `t2` left join `test`.`t` on((`test`.`t`.`c1` = `test`.`t2`.`c1`)) where 1
1 row in set (0.00 sec)
Notice that (A RIGHT JOIN B != A LEFT JOIN B) unless (A INNER JOIN B = A LEFT JOIN B). This is because A RIGHT JOIN B is not symmetrical with A LEFT JOIN B (it is symmetrical with B LEFT JOIN A).
In your case, A RIGHT JOIN B will be the same as A LEFT JOIN B unless there are NULL values in the columns you are joining. If there are NULL values, then A LEFT JOIN B will NOT be the same as A RIGHT JOIN B. If you add new articles without adding the associated newsgroup (or vice-versa) then the results would change too.

Codeproject has this image which explains the simple basics of SQL joins, taken from: http://www.codeproject.com/KB/database/Visual_SQL_Joins.aspx SQL joins explained

With your current data, yes they are the same. However as NEWSGROUP_ID is nullable then they could be different as the data changes.
Personally I always use LEFT JOINs if possible (from primary table to child table), in fact I have only needed to use a RIGHT JOIN a couple of times in over 6 years of SQL development!

Related

Multiple Selects on Insert Statement - mySQL

I have 2 tables.
1.
**code**
id
name
2.
**code_category**
id
code_id
category_id
discount
I have a list of 15000 codes in the database without the code_category yet. I only have 1 sample one to reference in code_category. I have to mass add in the code_category with 20 categories per code.
I am thinking of left joining both tables to get where code_id is null that gets me all the un-attached codes but I need another select statement to get the 20 different types of code_category rows to enter per code.
insert into code_category (code_id, category_id, discount)
values
(select id from code c
left join code_category cc on cc.code_id = c.id
where cc.code_id is null)
union
(select categoryid, discount from code_category where c.id = 123)
123 being the reference id.
I built a simplified example of your tables in a SQL Fiddle, then ran just the select (shown below) to see what rows would be inserted. See if this example answers your question.
Note how in the select I left join twice, once to get a list of categories, and again to find the missing ones.
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE IF NOT EXISTS `code` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
AUTO_INCREMENT=1
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
COMMENT '';
CREATE TABLE IF NOT EXISTS `code_category` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`codeid` INT(11) UNSIGNED NULL DEFAULT NULL,
`categoryid` INT(11) UNSIGNED NULL DEFAULT NULL,
`discount` INT(11) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_codecategory_catid` (`categoryid`),
KEY `idx_codecategory_codeid` (`codeid`)
)
ENGINE=MyISAM
AUTO_INCREMENT=1
DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci
COMMENT '';
INSERT INTO `code`
(`id`,`name`)
VALUES
(1,'code1'),
(2,'code2'),
(3,'code3'),
(4,'code4');
INSERT INTO `code_category`
(`codeid`,`categoryid`,`discount`)
VALUES
(1,1,3),
(2,1,2),
(1,2,2),
(1,3,3),
(1,4,2),
(1,5,2),
(1,6,2),
(1,7,2),
(1,8,2),
(1,9,2),
(1,10,2),
(1,11,2),
(1,12,2),
(1,13,2),
(1,14,2),
(1,15,2),
(1,16,2),
(1,17,2),
(1,18,2),
(1,19,2),
(1,20,2);
Query 1:
SELECT
c.id,
cc1.codeid,
cc1.categoryid,
cc1.discount
FROM code c
LEFT JOIN code_category cc1
ON cc1.codeid = 1
LEFT JOIN code_category cc
ON cc.codeid = c.id AND cc.codeid = cc1.codeid
WHERE cc.codeid is null
ORDER BY c.id,cc1.codeid,cc1.categoryid
Results:
| id | codeid | categoryid | discount |
|----|--------|------------|----------|
| 2 | 1 | 1 | 3 |
| 2 | 1 | 2 | 2 |
| 2 | 1 | 3 | 3 |
| 2 | 1 | 4 | 2 |
| 2 | 1 | 5 | 2 |
| 2 | 1 | 6 | 2 |
| 2 | 1 | 7 | 2 |
| 2 | 1 | 8 | 2 |
| 2 | 1 | 9 | 2 |
| 2 | 1 | 10 | 2 |
| 2 | 1 | 11 | 2 |
| 2 | 1 | 12 | 2 |
| 2 | 1 | 13 | 2 |
| 2 | 1 | 14 | 2 |
| 2 | 1 | 15 | 2 |
| 2 | 1 | 16 | 2 |
| 2 | 1 | 17 | 2 |
| 2 | 1 | 18 | 2 |
| 2 | 1 | 19 | 2 |
| 2 | 1 | 20 | 2 |
| 3 | 1 | 1 | 3 |
| 3 | 1 | 2 | 2 |
| 3 | 1 | 3 | 3 |
| 3 | 1 | 4 | 2 |
| 3 | 1 | 5 | 2 |
| 3 | 1 | 6 | 2 |
| 3 | 1 | 7 | 2 |
| 3 | 1 | 8 | 2 |
| 3 | 1 | 9 | 2 |
| 3 | 1 | 10 | 2 |
| 3 | 1 | 11 | 2 |
| 3 | 1 | 12 | 2 |
| 3 | 1 | 13 | 2 |
| 3 | 1 | 14 | 2 |
| 3 | 1 | 15 | 2 |
| 3 | 1 | 16 | 2 |
| 3 | 1 | 17 | 2 |
| 3 | 1 | 18 | 2 |
| 3 | 1 | 19 | 2 |
| 3 | 1 | 20 | 2 |
| 4 | 1 | 1 | 3 |
| 4 | 1 | 2 | 2 |
| 4 | 1 | 3 | 3 |
| 4 | 1 | 4 | 2 |
| 4 | 1 | 5 | 2 |
| 4 | 1 | 6 | 2 |
| 4 | 1 | 7 | 2 |
| 4 | 1 | 8 | 2 |
| 4 | 1 | 9 | 2 |
| 4 | 1 | 10 | 2 |
| 4 | 1 | 11 | 2 |
| 4 | 1 | 12 | 2 |
| 4 | 1 | 13 | 2 |
| 4 | 1 | 14 | 2 |
| 4 | 1 | 15 | 2 |
| 4 | 1 | 16 | 2 |
| 4 | 1 | 17 | 2 |
| 4 | 1 | 18 | 2 |
| 4 | 1 | 19 | 2 |
| 4 | 1 | 20 | 2 |
|----|--------|------------|----------|
You may need to change the column names to match your tables.

how to select a list from random id to the last

im new with SQL. i know to how select a list with limit comand. but that way need a value to select. what if i want select a list from random id to the last. exemple:
I want to select a list with id from 4 -> last of row (cuz i dont know whats last id)
select * from thing1 where id>=4 order by rand();
where thing1 is your table name. How you seed your random number generator (RNG) is up to you.
+----+---------+------------+
| id | conn_id | read_date |
+----+---------+------------+
| 11 | 3 | 2013-02-21 |
| 5 | 1 | 2012-02-21 |
| 8 | 5 | 2010-12-21 |
| 15 | 7 | 2019-12-21 |
| 14 | 6 | 2019-12-21 |
| 13 | 5 | 2016-02-21 |
| 4 | 2 | 2010-12-21 |
| 7 | 2 | 2014-02-21 |
| 6 | 2 | 2007-12-21 |
| 12 | 4 | 2014-02-21 |
| 16 | 8 | 2010-12-21 |
| 9 | 3 | 2010-12-21 |
| 10 | 4 | 2010-12-21 |
+----+---------+------------+
13 rows in set (0.14 sec)
mysql> select * from thing1 where id>=4 order by rand();
+----+---------+------------+
| id | conn_id | read_date |
+----+---------+------------+
| 13 | 5 | 2016-02-21 |
| 6 | 2 | 2007-12-21 |
| 10 | 4 | 2010-12-21 |
| 16 | 8 | 2010-12-21 |
| 14 | 6 | 2019-12-21 |
| 5 | 1 | 2012-02-21 |
| 7 | 2 | 2014-02-21 |
| 11 | 3 | 2013-02-21 |
| 12 | 4 | 2014-02-21 |
| 4 | 2 | 2010-12-21 |
| 8 | 5 | 2010-12-21 |
| 9 | 3 | 2010-12-21 |
| 15 | 7 | 2019-12-21 |
+----+---------+------------+
13 rows in set (0.02 sec)
mysql> select * from thing1 where id>=4 order by rand();
+----+---------+------------+
| id | conn_id | read_date |
+----+---------+------------+
| 10 | 4 | 2010-12-21 |
| 4 | 2 | 2010-12-21 |
| 6 | 2 | 2007-12-21 |
| 7 | 2 | 2014-02-21 |
| 5 | 1 | 2012-02-21 |
| 9 | 3 | 2010-12-21 |
| 12 | 4 | 2014-02-21 |
| 16 | 8 | 2010-12-21 |
| 8 | 5 | 2010-12-21 |
| 15 | 7 | 2019-12-21 |
| 13 | 5 | 2016-02-21 |
| 14 | 6 | 2019-12-21 |
| 11 | 3 | 2013-02-21 |
+----+---------+------------+
13 rows in set (0.05 sec)
Stored Proc
To have starting random position, until the end, random ordering
-- drop procedure getRandomStartToEnd;
delimiter $$
create procedure getRandomStartToEnd()
BEGIN
declare theCount int;
declare theStart int;
select count(*) into theCount from thing1;
set #theStart:=floor((rand()*#theCount)+1);
select * from thing1 where id>=#theStart order by rand();
END
$$
call getRandomStartToEnd; -- call stored proc

How to delete from a table interlinking 4 tables

Hi following are my 4 tables.
client_parent_question :-
+----+------------+------------+---------+-----+------+------+
| id | is_deleted | sort_order | version | cid | pid | qid |
+----+------------+------------+---------+-----+------+------+
| 1 | | 1 | 0 | 1 | 1 | 1 |
| 2 | | 2 | 0 | 1 | 1 | 2 |
| 3 | | 3 | 0 | 1 | 1 | 3 |
| 4 | | 4 | 0 | 1 | 1 | 4 |
| 5 | | 1 | 0 | 1 | 2 | 7 |
+----+------------+------------+---------+-----+------+------+
mysql> select * from client_parent;
+----+------------+------------+---------+-----+------+
| id | is_deleted | sort_order | version | cid | pid |
+----+------------+------------+---------+-----+------+
| 1 | | 1 | 0 | 1 | 1 |
| 2 | | 2 | 0 | 1 | 2 |
+----+------------+------------+---------+-----+------+
2 rows in set (0.00 sec)
mysql> select * from client_question;
+----+------------+---------+-----+------+------+
| id | is_deleted | version | cid | pqid | qtid |
+----+------------+---------+-----+------+------+
| 1 | | 0 | 1 | 1 | 1 |
| 2 | | 0 | 1 | 2 | 4 |
| 3 | | 0 | 1 | 2 | 4 |
| 4 | | 0 | 1 | 1 | 1 |
| 5 | | 0 | 1 | 2 | 4 |
| 6 | | 0 | 1 | 3 | 4 |
| 7 | | 0 | 1 | 3 | 4 |
| 8 | | 0 | 1 | 1 | 1 |
| 9 | | 0 | 1 | 2 | 4 |
| 10 | | 0 | 1 | 3 | 4 |
| 11 | | 0 | 1 | 4 | 4 |
| 12 | | 0 | 1 | 4 | 4 |
+----+------------+---------+-----+------+------+
mysql> select * from client_question_option;
+----+------------+---------+------+------+
| id | is_deleted | version | cqid | oid |
+----+------------+---------+------+------+
| 1 | | 0 | 2 | 1 |
| 2 | | 0 | 3 | 4 |
| 3 | | 0 | 6 | 2 |
| 4 | | 0 | 7 | 3 |
| 5 | | 0 | 11 | 1 |
| 6 | | 0 | 12 | 4 |
| 7 | | 0 | 14 | 1 |
| 8 | | 0 | 15 | 4 |
+----+------------+---------+------+------+
I know only cid and pid of client_parent table
My aim is to delete all from client_question,client_parent_question and client_question_option
In the client_question_option cqid id is the id of client_question table
Following is the sqlfiddle
I did
DELETE FROM cqo,qo,cpq client_question_option cqo ,client_question cq,client_parent_question ,client_parent cp
WHERE cqo.cqid=cq.id AND cq.pqid=pq.id AND cqo.oid=qo.id AND cq.cid=1 AND cp.pid=1
But this did not work.
You just need to move the FROM keyword after the table aliases. See multiple-table syntax
DELETE cqo, qo, cpq
FROM client_question_option cqo,
client_question cq,
client_parent_question,
client_parent cp
WHERE cqo.cqid=cq.id AND cq.pqid=pq.id AND cqo.oid=qo.id
AND cq.cid=1 AND cp.pid=1;
Your DELETE query's syntax is wrong, which must be somthing like this:-
DELETE cqo,cp,pq
FROM client_question_option cqo, client_question cq, client_parent_question pq, client_parent cp
WHERE cqo.cqid=cq.id AND cq.pqid=pq.id AND cqo.oid=cp.id AND cq.cid=1 AND cp.pid=1;
Hope this will help you.
using inner join
DELETE cqo,cq,cp,pq
FROM client_question_option cqo
INNER JOIN client_question cq
INNER JOIN client_parent_question as pq
INNER JOIN client_parent cp
WHERE cqo.cqid=cq.id AND cq.pqid=pq.id AND cqo.oid=cqo.id AND cq.cid=1 AND cp.pid=1
You have an option in MYSQL Called as ONDELETE CASCADE where in when you set this on a key
example : foreign key(cid) references client_parent(cid) ON DELETE CASCADE when you delete the key based on your condition all referencing ids will be deleted , so one simple query to delete parent row is enough for it to delete all child rows present in other table data
Advantages of using ON DELETE CASCADE
you are not require to right innerjoins and delete data manually
you dont get parent key errors
you can save up your time
Thats it NJOY!!!

fetching required data using joins in mysql

My tables :
mysql> select * from professor;
+-------+--------+--------+--------+------+
| empid | name | status | salary | age |
+-------+--------+--------+--------+------+
| 1 | Arun | 1 | 2000 | 23 |
| 2 | Benoy | 0 | 3000 | 25 |
| 3 | Chacko | 1 | 1000 | 36 |
| 4 | Divin | 0 | 5000 | 32 |
| 5 | Edwin | 1 | 2500 | 55 |
| 7 | George | 0 | 1500 | 46 |
+-------+--------+--------+--------+------+
6 rows in set (0.00 sec)
mysql> select * from works;
+----------+-------+---------+
| courseid | empid | classid |
+----------+-------+---------+
| 1 | 1 | 10 |
| 2 | 2 | 9 |
| 3 | 3 | 8 |
| 4 | 4 | 10 |
| 5 | 5 | 9 |
| 6 | 1 | 9 |
| 2 | 3 | 10 |
| 2 | 1 | 7 |
| 4 | 2 | 6 |
| 2 | 4 | 6 |
| 2 | 5 | 2 |
| 7 | 5 | 6 |
| 3 | 5 | 2 |
| 6 | 4 | 10 |
+----------+-------+---------+
14 rows in set (0.00 sec)
mysql> select * from course;
+----------+------------+--------+
| courseid | coursename | points |
+----------+------------+--------+
| 1 | Maths | 5 |
| 2 | Science | 1 |
| 3 | English | 6 |
| 4 | Social | 4 |
| 5 | Malayalam | 20 |
| 6 | Arts | 25 |
| 7 | Biology | 20 |
+----------+------------+--------+
7 rows in set (0.00 sec)
Question is :
Return the name(s) of the professor(s) who taught the most number of
courses in Class 10
Query i tried is :
select professor.name,works.courseid,works.empid,works.classid from professor
inner join works
on professor.empid=works.empid
where works.classid=10
group by works.courseid
I know its imcomplete/incorrect. Pls help me to the required result.
select
professor.name, count(works.courseid)
from
works
inner join
professor on
professor.empid = works.empid
where
work.classid = 10
group by
professor.name
order by count(works.courseid) desc
limit 1
change this in your select statment
works.courseid
to
count(works.courseid) as courseid

What is self join in mysql used for

What is this self join and why do we need this self join?. I have till date never used self joins.
See if these links helps you...
http://www.udel.edu/evelyn/SQL-Class3/SQL3_self.html
http://awads.net/wp/2006/07/11/back-to-basics-self-joins/
http://www.sqltutorial.org/sqlselfjoin.aspx
Good Luck!!!
there are number of reasons, and tons of examples are available on web
http://www.udel.edu/evelyn/SQL-Class3/SQL3_self.html
mysql> SELECT * FROM pr WHERE id>80;
+----+------+--------+
| id | ids | status |
+----+------+--------+
| 81 | 4 | 4 |
| 82 | 2 | 3 |
| 83 | 2 | 4 |
+----+------+--------+
3 rows in set (0.00 sec)
mysql> SELECT * FROM pr WHERE id<18;
+----+------+--------+
| id | ids | status |
+----+------+--------+
| 1 | 1 | 1 |
| 5 | NULL | 2 |
+----+------+--------+
2 rows in set (0.01 sec)
identical requests :
mysql> SELECT * FROM pr AS t1 ,pr AS t2 WHERE t1.id<18 AND t2.id>80;
+----+------+--------+----+------+--------+
| id | ids | status | id | ids | status |
+----+------+--------+----+------+--------+
| 1 | 1 | 1 | 81 | 4 | 4 |
| 5 | NULL | 2 | 81 | 4 | 4 |
| 1 | 1 | 1 | 82 | 2 | 3 |
| 5 | NULL | 2 | 82 | 2 | 3 |
| 1 | 1 | 1 | 83 | 2 | 4 |
| 5 | NULL | 2 | 83 | 2 | 4 |
+----+------+--------+----+------+--------+
6 rows in set (0.00 sec)
mysql> SELECT * FROM pr AS t1 JOIN pr AS t2 ON t1.id<18 AND t2.id>80;
+----+------+--------+----+------+--------+
| id | ids | status | id | ids | status |
+----+------+--------+----+------+--------+
| 1 | 1 | 1 | 81 | 4 | 4 |
| 5 | NULL | 2 | 81 | 4 | 4 |
| 1 | 1 | 1 | 82 | 2 | 3 |
| 5 | NULL | 2 | 82 | 2 | 3 |
| 1 | 1 | 1 | 83 | 2 | 4 |
| 5 | NULL | 2 | 83 | 2 | 4 |
+----+------+--------+----+------+--------+
6 rows in set (0.00 sec)