mysql search where 3 columns for row have the same value - mysql

id - in_id - nat_id
1 1 1
2 1 3
3 3 3
4 2 1
Is it possible to select with mysql only the values in the above table which are the same across the 3 columns, ie return 1 and 3?
Or is this kind of filter only possible post query with php?
Thanks,
John

This simple query should work for you:
SELECT id
FROM your_table
WHERE id = in_id
AND nat_id = in_id
;
Check example at SQLFiddle: SQLFiddle Example

Related

How can I replace nested for statement with mysql query?

Environment :
MySQL 5.7.x
Spring MVC
Table Data (name: TableA)
seq
level
name
order
parent_seq
1
1
name1
1
0
2
1
name2
2
0
3
2
sub1-1
1
1
4
2
sub1-2
2
1
5
2
sub2-1
1
2
6
3
third-2-1
1
5
7
3
third-1-1
1
3
Expected Result
seq
level
name
order
parent_seq
next_level
1
1
name1
1
0
2
3
2
sub1-1
1
1
3
7
3
third-1-1
1
3
2
4
2
sub1-2
2
1
1
2
1
name2
2
0
2
5
2
sub2-1
1
2
3
6
3
third-2-1
1
5
1 (last default value: 1)
Now I'm genenrating expected result with nested for statement(JAVA).
Is there any way to generate expected result only with MySQL Query?
The data stacked in random order in the table is sorted by ASC based on the level column, but check the parent_seq column so that it is sorted under the parent data. And if there are multiple data of the same level, sort by ASC based on the sort column value.
Thanks in advance!
++
EmbraceNothingButFuture's answer was great, but the query seems to work on MySQL 8. I'm using MySQL 5.7. Is there any way to use the query on MySQL 5.7?
Summary:
Use REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*") to extract the numbers and sort the datas using the numbers.
For MySQL v8 above, you can use LEAD() to generate the "next_level" column based on the "level" column
COALESCE() function for the last default value = 1
SELECT
t1.*,
COALESCE(LEAD(t1.level, 1) OVER(ORDER BY REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*")), 1) AS next_level
FROM TableA t1
ORDER BY REGEXP_SUBSTR(name,"[0-9]+\-?[0-9]*"), t1.level
See db<>fiddle

get next row of current mysql

I have table like this:
ID NAME
1 juan
2 pedro
3 jose
4 lucas
5 antoni
I need show result like this:
1 juan
3 jose
5 antoni
How should I proceed to show every record in a sequence of 2 ?
Thank you
Use Modulo(%) operator
Select * from yourtable Where ID % 2 = 1
This considers ID is sequential
SELECT * FROM
(
SELECT ID,NAME,ROW_NUMBER()OVER(ORDER BY ID ASC)as ROW
FROM TABLE
) as A
WHERE ROW % 2 = 0

SQL query for incrementing values of row data through sequencing

I have row and column locations of several students.
Assuming number of rows and columns are fixed (to 3x3), how can I have a query result listing all row and column combinations, with students mapped to the correct location?
For example given these students data:
Student Row Column
Paul 1 1
Chris 1 3
James 2 2
Dwayne 3 3
How to have a query output like this:
Student Row Column
Paul 1 1
NULL 1 2
Chris 1 3
NULL 2 1
James 2 2
NULL 2 3
NULL 3 1
NULL 3 2
Dwayne 3 3
Please help! Thank you very much in advance.
While using PHP, Try mysql_insert_id() for your Code.
See Example here:
http://php.net/manual/en/function.mysql-insert-id.php
Good luck.
First of all, you need to know that Mysql haven't a implicit generator of N numbers of rows, like other RDBMS have, but you can emulate this using something like this:
http://use-the-index-luke.com/blog/2011-07-30/mysql-row-generator#mysql_generator_code
Take a look for study porpuse.
But for a first approach to resolve your problem, you can try this:
SELECT IFNULL((SELECT STUDENT FROM StudentSeatPlan B WHERE B.ROW = TB.ROW_ AND B.COLUMN = TB.COLUMN_),'') AS STUDENT,
TB.ROW_,TB.COLUMN_
FROM (
SELECT 1 ROW_,1 COLUMN_ UNION ALL
SELECT 1,2 UNION ALL
SELECT 1,3 UNION ALL
SELECT 2,1 UNION ALL
SELECT 2,2 UNION ALL
SELECT 2,3 UNION ALL
SELECT 3,1 UNION ALL
SELECT 3,2 UNION ALL
SELECT 3,3) TB
Whatever, it seems like you have a schema problem, something wrong it happens that you need generate data in this form in Mysql, maybe you prefered make it in your app if is the case.

query the same field multiple times in the same query

I need to filter a table in mysql but can't get past the beginning.
The table has 2 fields:
ID_house house_feature
1 1
1 2
1 4
1 5
2 1
2 3
2 4
3 1
3 2
3 3
I need to filter this table using the following parameters:
house feature = 1
AND
house feature = 2
AND
house feature = 3
So that I get all houses with the requested feature.
I already tried to create something similar to this:
SELECT *
FROM houses
WHERE
house_feature = 1
AND
house_feature = 2
AND
house_feature = 3
But it doesn't work as I expected.
Is there a way to get this result with MySQL?
It seems that I acn filter the table using only the OR operator but this way I can't get the right result.
Thanks in advance for any help.
tony
You can do so ,by matching the distinct count of features per house ,so the house with exactly these 3 features will be returned
SELECT *
FROM t
WHERE
house_feature IN(1 ,2,3)
group by ID_house
having count(distinct house_feature) = 3
Demo

MySQL: Use data from one table to fill a second table using phpMyAdmin

SQL conundrum here.
I want to populate an empty table based on the data in an existing table using phpMyAdmin.
More specifically, I want to use the data in mark to create the data in attempt. The columns in mark are student_number, test_number, attempt_number, question_number and the answer. It's a multiple-choice test analysis tool.
mark (existing)
snum tnum anum qnum answer
----------------------------------------
1 1 1 1 A
1 1 1 2 C
1 1 1 3 D
1 1 2 1 B
1 1 2 2 A
1 1 2 3 C
attempt (to be created)
snum tnum anum period
--------------------------------
1 1 1 2013-1
1 1 2 2013-2
I can get the distinct snum, tnum, anum combinations as follows:
SELECT DISTINCT snum, tnum, anum FROM `mark`
How can I use the results from this call to populate the requisite insert call?
INSERT INTO `attempt` (snum,tnum,anum,period) VALUES (:s,:t,:a,"2013-1")
Ideally, I'd like to auto-complete the period value based on "2013-" plus the anum. I suspect this is not possible, so I'll just select all the anum=1 values, and hardcode the period value (and then repeat for each anum).
Thanks.
Use the "INSERT INTO SELECT" syntax:
INSERT INTO 'attempt'(snum,tnum,anum,period) SELECT DISTINCT snum,tnum,anum,CONCAT('2013-',anum) as period FROM mark;
INSERT INTO `attempt`
(snum,
tnum,
anum,
period)
select DISTINCT
snum,
tnum,
anum,
concat('2013-',anum) as period
from mark;