I have table like this:
id products
------ ----------
5 1,2,3
6 2,4,5
9 1,4,7
17 4,6,7
18 1,6,8
19 2,3,6
I have to select only that rows, which row's products column contains one of (2,3) values.
In this case query must return:
id products
------ ----------
5 1,2,3
6 2,4,5
19 2,3,6
But I don't understand how to make construction of this query.
Any ideas?
Thanks in advance.
SELECT id,products
FROM yourTable
WHERE FIND_IN_SET('2',products)>0
OR FIND_IN_SET('3',products)>0
sqlFiddle
Would you mind to try this one please?
select * from TABLE_NAME where products regexp "(^|,)[23](,|$)";
Its doing either two or three at the begining, or at end. Or in between the commas.
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table. Then you can select normally.
Your table should look like this
id product
-- -------
5 1
5 2
5 3
6 2
6 4
6 5
...
With that table structure your select would be
select id
from your_normalized_table
where product in (2,3)
group by id
having count(distinct product) = 2
That query can make use of indexes and is really fast.
Related
I have table where the data is like bellow
Combination_id combination
1 1
1 3
1 4
2 1
2 3
2 5
I want to find the combination_id based on combination.
Test case
1 . For the combination of 1,3,4 I should get combination id 1
2 . For the combination of 1,3,5 I should get combination id 2
I tried many MySQL queries but failed. Can anyone help me here??
Thanks in advance...
There are many ways, but it depends on how your combination is used
For example
select t1.Combination_id
from tab1 t1
where t1.combination in (1,3,4)
group by t1.Combination_id
having count(1) = 3
I know this is probably so odd to ask. But lets say I have 3 tables:
Table 1
ID
Name
1
Adam
2
David
3
Conor
Table 2
ID
Name
1
Adam
2
Derek
3
Niall
Table 3
ID
Name
1
Adam
2
David
3
John
Is there any way I can write a query to get the unique names across all 3 tables. So it would return "Adam, David, Conor, Derek, Niall, John"
Order doesn't matter
If it helps, all name values are related to a names table
yes , one way is to union them
select name from table1
union
select name from table2
union
select name from table3
union automatically removes duplicate cases
I have a MySQL table which contains val_type column which have 3 type of values
id val_type company
1 rib 1
2 mod 2
3 rib 2
4 rib 3
5 mod 1
6 trop 1
$res= SELECT SUM(val_type) from tabl_name GROUP BY company;
with above query I get sum of all types in one
Result Required : Rib=3, mod=2 and trop=1
I want to get sum of all three types with one MySQL query. like how many rib,mod and trop.
Thanks
It sounds like you want to count all three types. You only need a basic GROUP BY query:
SELECT
val_type,
COUNT(*) AS cnt
FROM tabl_name
GROUP BY
val_type;
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.
In MySQL I have one particular cell with data something like this
5,6,7,8,9
If I need to search for specific 2 numbers one after another I do a query with LIKE statement for those 2 particular numbers. For ex. I need to check if there's a row with numbers 6 & 7 *in a row* I do
SELECT * FROM table WHERE column LIKE '%,6,7,%' OR column LIKE '%,6,7' OR column LIKE '6,7,%'
It's little redundant and clumsy. If I 'convert' those numbers into multiple rows, for ex. every number would become it's own row with column 'numbers' ordered with 'sort' column so I know the order of rows.
id numbers sort
55 8 4
56 6 2
57 5 1
58 7 3
59 9 5
...
What's the identical query for this case? So I would have the same result as with the query above. I need to order the query with sort column and check if the numbers 6,7 are occurring one after another with that sorting.
Should be something like this. (if I understood right your problem). It will return nothing if the 2 numbers are not in sequence by the sort column.
select *
from table t1
join table t2 on t1.sort=t2.sort+1
where t1.numbers=6 and t2.numbers=7
if you do not know which one should be first you can use it like this:
select *
from table t1
join table t2 on t1.sort=t2.sort+1 or t1.sort+1=t2.sort
where t1.numbers=6 and t2.numbers=7
Are the numbery always incremented by one or can they have any value?
I'm proposing the following table structure
id col1 col2 rowid
1 1 2 1
2 2 3 1
3 1 4 2