SELECT * FROM table WHERE (my_id) IN table_column (assigned_id) - php

I have a table called tasks Where has a column named assigned_id which has comma separated values like (1, 2, 3, 4, 5). I need to count of the tasks user has assigned.
In Example: My name is Imdad and my user_id is 5, and the tasks table has column assigned_id which values are like 1, 3, 4, 5, 7... I need to count all the number of rows which assigned_id contain my user_id which is 5.
I have tried so many methods but not working till now.
I have tried
SELECT * FROM tasks WHERE $user_id IN (assigned_id);
Here the user_id is 5
It's returning if the assign_id value start with 5 (Example: 5, 3, 6, 7...)
Here is my code
<?php
$data = "SELECT COUNT('1') FROM task WHERE $user[id] IN (assigned_id)";
$get_data = mysqli_query($conn, $data);
$show = mysqli_fetch_array($get_data);
echo $show[0];
?>
I am attaching a screenshot for better understand!!!
Here I have marked the data which I want
But Here is the image with query, I got the first value

As per my understanding on the above query this should work for you in MYSQL
if you want to get based on task_title
Select count(*) from tasks where assignee_id LIKE (CONCAT('%', (Select id from tasks where task_title = 'Imdad' LIMIT 1), '%'));
if you want to get based on id
Select count(*) from tasks where assignee_id LIKE (CONCAT('%', (Select id from tasks where id = 5 LIMIT 1), '%'));
Hope this helps.

In such case you can use find_in_set function like:
select *
from tbl
where find_in_set(5, assigned_id);
SQL online editor

Related

MySQL - query for duplicates of a column and return both original and duplicate rows

I have a table that I use to store some systematically chosen "serial numbers" for each product that is bought...
The problem is, a CSV was uploaded that I believe contained some duplicate "serial numbers", which means that when the application tries to modify a row, it may not be modifying the correct one.
I need to be able to query the database and get all rows that are a double of the serial_number column. It should look something like this:
ID, serial_number, meta1, meta2, meta3
3, 123456, 0, 2, 4
55, 123456, 0, 0, 0
6, 345678, 0, 1, 2
99, 345678, 0, 1, 2
So as you can see, I need to be able to see both the original row and the duplicate row and all of it's columns of data ... this is so I can compare them and determine what data is now inconsistent.
Some versions of MySQL implement in with a subquery very inefficiently. A safe alternative is a join:
SELECT t.*
FROM t join
(select serial_number, count(*) as cnt
from t
group by serial_number
) tsum
on tsum.serial_number = t.serial_number and cnt > 1
order by t.serial_number;
Another alternative is to use an exists clause:
select t.*
from t
where exists (select * from t t2 where t2.serial_number = t.serial_number and t2.id <> t.id)
order by t.serial_number;
Both these queries (as well as the one proposed by #fthiella) are standard SQL. Both would benefit from an index on (serial_number, id).
SELECT *
FROM
yourtable
WHERE
serial_number IN (SELECT serial_number
FROM yourtable
GROUP BY serial_number
HAVING COUNT(*)>1)
ORDER BY
serial_number, id

find entries to the left and right of a certain entry in a mysql table in a single query

Let's say I have a table in a database like this table. Let's say that I want to get the entries on the left and right of the entry whose the primary key is equal to 5 (or any other primary key). So in our case, I want to get the entries whose primary key is equal to 4 and 6 respectively. What is the SQL query that will give me such result? Can you guys translate the SQL query into a find('all') CakePHP query?
Thank you
NOTE: The ids are not necessarily contiguous, meaning, they do not necessarily follow the 1, 2, 3, 4, 5, 6, 7, 8 sequence. I can have something like 1, 5, 13, 44, 66, 123, etc
Try Union like this
(SELECT * FROM employee WHERE id < 5 order by id DESC LIMIT 1)
UNION
(SELECT * FROM employee WHERE id >5 LIMIT 1)
PHP
$id = 5;
SELECT * FROM Employee where id = $id-1 OR id = $id+1;
MySQL
SET #id = 5;
SELECT * FROM Employee where id = #id-1 OR id = #id+1;
Checkout find('neighbors'). It returns the records before and after the one you specify and your ids can have "holes" in the sequence.

Is there a way to specify order of results manually in SQL?

I have a PHP array of IDs of rows to retrieve from a MySQL database.
$ids = array(35, 20, 1, 5);
Is there any way I could construct an SQL query that retrieves a resource from the database with rows in that specific order of IDs?
In other words, the list of rows returned would have row with ID 35 first, then the row with ID of 20, and so on.
Try this one
<?php
$ids = array(35, 20, 1, 5);
$q = 'select * from table where id in ('.implode(',',$ids).') order by find_in_set(id, \''.implode(',',$ids).'\')' ;
Sorting on PHP/Javascript end would be easier, but one way comes to mind how you could do it in SQL:
select * from whatever where id in (35,20,1,5) order by
case id when 35 then 0 when 20 then 1 when 1 then 2 when 5 then 3 end

Select count for each distinct row (mysql and php)

I am using mysql and php.
I have a table with one column. I can show unique rows by:
select distinct id from id_table
This might show
1
2
3
5
What I want to do is show the number of 1's, 2's, etc there are.
I could do
select count(id) from id_table where id = 1
But then I have to run a query for every... single... row... And with hundreds of thousands of rows, not good.
Is there a way to return an array of the counts of each distinct item
Data
1, 1, 1, 2, 3, 3, 5
Results
3, 1, 2, 1
Thanks
select id, count(id)
from table
group by id
If only want count of ids, then
select count(id)
from table
group by id
Here is a simple tutorial of group by clause
http://www.w3schools.com/sql/sql_groupby.asp

mysql query ORDER BY supplied condition

SELECT * FROM newmessage
ORDER BY id <somecondition>
here my condition is like :
(5, 3, 2, 1, 4)
ie. i want to ORDER the result according to id like my given order above.
SELECT * FROM newmessage
WHERE id IN (5, 3, 2, 1, 4)
ORDER BY FIELD(id, 5, 3, 2, 1, 4)
UNION
SELECT * FROM newmessage WHERE id NOT IN (5, 3, 2, 1, 4)
You need to use a CASE statement in the ORDER BY clause. It's a bit awkward, but does the job:
SELECT * FROM newmessage m
ORDER BY CASE WHEN m.someColumn = 5 THEN 1
WHEN m.someColumn = 3 THEN 2
WHEN m.someColumn = 2 THEN 3
WHEN m.someColumn = 1 THEN 4
WHEN m.someColumn = 4 THEN 5
ELSE m.someColumn END
EDIT : I would add, mind you, that if you have do the ability to add an 'index' or 'sort' column to the NewMessage table, then you should. Whilst my CASE hack works, it's not pretty.
One way of doing this is the fallowing:
SELECT * FROM table ORDER BY id IN(7, 13, 12) DESC;
which will sort the rows with id 7, 12 and 13 first, it will not sort in the exact order that you list in the IN() statement.
Here's how you can sort in a specific order:
SELECT * FROM table ORDER BY id = 7 DESC, id = 13 DESC, id = 12 DESC;
which will give the rows in the order that you list them. Of course, the ASC/DESC part still applies here, I just assume you need this ids first. It's a little cumbersome, but I don't think there's a more elegant way to achieve this.
It sounds like you might want to look at ORDER BY FIELD.
It allows you to specify a custom order for a given field, so you might use like this:
SELECT someField,priority FROM myTable
ORDER BY FIELD (priority,'HIGH','MEDIUM','LOW')
Which would give you priorities in that order, rather than alphabetical as would normally happen there.
So an example for the query you supplied might be:
ORDER BY FIELD(id,'5','3','2','1','4')
You can sort like this in MySQL using FIELD:
SELECT *
FROM newmessage
ORDER BY FIELD(id, 5, 3, 2, 1, 4)
however:
this will probably not work as expected, when there are more rows in the table, then those specified in the ORDER clause. So you would either have to put all IDs in there or add a filter, if you want to only return these 5 rows.

Categories