Select only one number in multiple value in column - php

http://imageshack.com/a/img43/3586/p49t.jpg
i have select only 11 number in user_assign column.
so how to write MySQL query in PHP ...
Thanks

Use the FIND_IN_SET
SELECT * FROM table WHERE FIND_IN_SET('11', user_assign);

Related

I have comma seperated column and need to get most common values from them in mysql

I have comma separated value in mysql table field and want to get most common value amongst them.
i.e. I have a table name is A, In which there are two fields id and available_values.
id available_values
--- -----------------
1 3,5,7,9
2 3,5
3 5,9
In above example there are value 5 exist in all rows so i need to get this value(mean - 5), because this is available in every records.
Please help to find out the solution.
Thanks in advance.
Best way is to include another table with one row per id and your values. But you can use REGEXP to achieve the output :
SELECT * FROM table_name WHERE
available_values REGEXP CONCAT('(^|,)(', replace(5,',','|'),')(,|$)')
SELECT * FROM `test` WHERE FIND_IN_SET($keysValue,available_values)
example
SELECT * FROM `tableName` WHERE FIND_IN_SET($keysValue,columnsName)

Selecting 2 random rows from SQL table

What would be the fastest way to select 2 random rows from an SQL table ? (SQL only or not)
I am using MySQL on PhpMyAdmin.
Seems like it would be:
SELECT * FROM table LIMIT 2
What you will get is two rows in database default order.
Try this:
SELECT * FROM table ORDER BY RAND() LIMIT 0, 2

Mysql not select columns

I have a table contain 22 columns
I have a query need to SELECT 20 columns
Is any way to do a query like NOT SELECT (the columns i don't want to select)
So I don't need to type SELECT column1, columns2...
You cannot do it as you expected. You have to type all the columns you want. If you have run the query many times, you can create a VIEW with selected columns.
No it is not possible, the expression "select all except" or "NOT SELECT" has not yet been implemented in any existing database.
The only way is to specify the columns you want or use the '*' wildcard
SELECT * FROM TABLE
or
SELECT column1, column2...
SQL doesn't allow to hide some columns. You can either select all columns by using SELECT * ... or list columns you need by SELECT col1, col2, ...
Please check this answer. It is the only way to do that
Select all columns except one in MySQL?
(I can't comment so I put the link as an answer)
Cheers

SQL SELECT Question

I am trying to perform a SQL query that acts in two parts.
First, I have a query that returns a list of 10 Ids.
But then I want to have a SELECT statement which has a WHERE clause for each of these 10 ids.
Is this possible?
I tried:
SELECT * FROM tablenameWHERE id= (SELECT id FROM table_of_ids WHERE
tableid='1a177de1-3f25c9b7910b' OR
tableid='64faecca-133af807a65a' OR
... up to 10 Ids)
but it returns with an error stating the subquery returns more than 1 row.
Note, the tableid and id columns of table_of_ids are different values.
Does anyone know how to accomplish this?
I seem to be at a loss myself.
If it matters, I am using mySQL and PHP.
Cheers,
Brett
Not a very optimized query, but you could use IN instead of =
SELECT * FROM tablename WHERE id IN (SELECT id FROM table_of_ids WHERE
tableid='1a177de1-3f25c9b7910b' OR
tableid='64faecca-133af807a65a' OR
... up to 10 Ids)
Change it to in()
WHERE id IN (SELECT id ...)
Replace the = with the IN keyword.
select * from sometable where key in (subselect that returns one column)
Looks like you can;
SELECT *
FROM tablename
INNER JOIN table_of_ids ON table_of_ids.id = tablename.id
WHERE table_of_ids.tableid IN (
'1a177de1-3f25c9b7910b',
'64faecca-133af807a65a',
... )
Don't use the "=" operator, use the "IN" operator.
See this tutorial for examples.
Use IN keyword.
SELECT * FROM user WHERE Id IN(SELECT userId FROM table).
Id is primary key of your first table and userId is foreign key in second table of coz.

How to fetch the first 2 or 3 rows from Mysql DB using Mysql PHP function?

How to fetch the first two rows from Mysql DB using Mysql PHP function? Is there any function which can give me first 2 or 3 rows from the select query we fired?
Use LIMIT. From the manual, to retrieve 3 rows:
SELECT * FROM tbl LIMIT 3;
Or to retrieve rows 6-15:
SELECT * FROM tbl LIMIT 5,10;
For this query (i.e. with no constraint) if you are not using an ORDER BY clause your results will be ordered as they appear in the database.
You can use the limit clause in your query:
select * from your_table limit 3
This will select the first three rows.
And:
select * from your_table limit 5, 3
The later will select rows starting from 5 and return three rows.
You have 2 options:
Fire a query to select all the rows
and then select 2 or 3 rows as
needed using PHP.
Fire a query to select only the
necessary number of rows using LIMIT
clause.
The 2nd option is preferable.

Categories