我们可以使用不带循环的sql吗?
我们是否可以通过查询获得以下输出。。?这在sql中是可能的吗 我们有一个带有行号的表,例如从1到15的序列 我们还有另一个表,它包含从和到的行号,比如 根据第二个表,我们需要连接第一个表并获得如下输出: 那么,我们可以在不使用循环的情况下从SQL获得这种类型的输出吗 谢谢,我们可以使用不带循环的sql吗?,sql,Sql,我们是否可以通过查询获得以下输出。。?这在sql中是可能的吗 我们有一个带有行号的表,例如从1到15的序列 我们还有另一个表,它包含从和到的行号,比如 根据第二个表,我们需要连接第一个表并获得如下输出: 那么,我们可以在不使用循环的情况下从SQL获得这种类型的输出吗 谢谢, 女孩子气试试这样的比较: select Id, FROM_ROW from table2, table1 where table1.kId between table2.FROM_ROW and table2.TO_RO
女孩子气试试这样的比较:
select Id, FROM_ROW from table2, table1
where table1.kId between table2.FROM_ROW and table2.TO_ROW -1
您需要在表2中为第15行添加一行 我删除了不兼容的数据库标记。仅使用实际使用的数据库进行标记。
MariaDB [sandbox]> SELECT * FROM T;
+----------+--------+
| from_row | to_row |
+----------+--------+
| 1 | 4 |
| 4 | 6 |
| 6 | 9 |
| 9 | 13 |
+----------+--------+
4 rows in set (0.00 sec)
MariaDB [sandbox]> SELECT * FROM USERS;
+-----+----------+--------------+--------+---------------------+
| id | userName | photo | status | ts |
+-----+----------+--------------+--------+---------------------+
| 1 | John | john.png | 1 | 2016-12-08 13:14:24 |
| 2 | Jane | jane.png | 1 | 2016-12-08 13:14:24 |
| 3 | Ali | | 1 | 2016-12-08 13:14:24 |
| 6 | Bruce | bruce.png | 1 | 2016-12-08 13:14:24 |
| 7 | Martha | | 1 | 2016-12-08 13:14:24 |
| 8 | Sidney | | 1 | 2016-12-08 13:14:24 |
| 10 | Charlie | charlie.png | 1 | 2016-12-08 13:14:24 |
| 12 | Elisa | | 1 | 2016-12-08 13:14:24 |
| 14 | Samantha | samantha.png | 1 | 2016-12-08 13:14:24 |
| 15 | Hannah | hannah.png | 1 | 2016-12-08 13:14:24 |
| 16 | Hannah | | 1 | 2016-12-08 13:14:24 |
| 17 | Kevin | kevin1.png | 1 | 2016-12-08 13:14:24 |
| 18 | Kevin | kevin2.png | 1 | 2016-12-08 13:14:24 |
| 19 | Ruth | | 1 | 2016-12-08 13:14:24 |
| 999 | xxx | photo | 1 | 2016-12-08 13:16:41 |
+-----+----------+--------------+--------+---------------------+
15 rows in set (0.00 sec)
MariaDB [sandbox]> SELECT U.ID, FROM_row as start_row
-> FROM USERS U
-> JOIN T ON U.ID
-> BETWEEN T.FROM_row AND T.TO_row
-> ;
+----+-----------+
| ID | start_row |
+----+-----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 6 | 4 |
| 6 | 6 |
| 7 | 6 |
| 8 | 6 |
| 10 | 9 |
| 12 | 9 |
+----+-----------+
9 rows in set (0.00 sec)