How to match CSV value in column - php

my mysql table name products.
How can get data this table.
table shema
id| catid |subcatid
-----------------
1 | 5 | 2,3,5
SELECT * FROM products where subcatid=2
not correct
how can write correct sql.

SELECT * FROM products where find_in_set(2,subcatid)
consider to normalize your table

Related

Get all row had value in column

How do I get all rows having the value 1, but not 11 in column news_short?
Here's my table:
id | news_short |
1 | 1,2,3,4,5,6,7 |
2 | 2,4,5,6,1,5,6 |
3 | 11,2,5,6,9,4 |
I agree you should normalize. But here is the solution the way it sits.
Select * from table where news_short like '%1,%' and
news_short not like '%11,%';
FIND_IN_SET() returns the position of a string if it is present (as a substring) within a list of strings
so you should search if a value is != 11
eg:
->where("FIND_IN_SET(news_short) !=", 11)
Your best bet would be normalize your schema do not store relations in form of comma separated list instead create a junction table to maintain a m:m many to many or if its one to many relation between main table and these news_short (probably from other table) values,create a new table as news_short with columns main table id and news(table) id and in each row save one association per your_table and news.
If you aren't able to update/change your schema you could use find_in_set() but its not a good option
select *
from your_table
where find_in_set(1, news_short) > 0
demo

mysql linked list sort query, why is it working?

My table looks like this:
+------------------------+
| id | title | position |
+------------------------+
| 1 | test 2 | 3 |
+------------------------+
| 2 | test 3 | 1 |
+------------------------+
| 3 | test 1 | 0 |
+------------------------+
I found this query which retrieves the rows ordered based on the position field which holds the id of the predecessor.
SELECT
*
FROM
mytable AS t1
LEFT JOIN
mytable AS t2
ON t2.position = t1.id
I wonder why this is working because there is no order by clause and the database should't know that position 0 is the row to start at.
The result is dependent on the order you inserted the rows into the table. If, for example, you had inserted the row with id=3 before you inserted the row with id=2, then you would have got a non-sorted result.
As it stands, you are pulling the data out of t1 in the order of id because that is the order you put the elements into the table
See http://sqlfiddle.com/#!2/63a925/2 and try it for yourself.
N.B. Databases are not guaranteed to work as you state, it is simply that most databases work this way. You should not rely on this behaviour as a minor change to the schema or query could ruin your whole day! Note also that if id is a (primary?) key, the insert order will probably be overridden by the fact that the database will pull the rows out in the order of the index.
That query is joining in table 2 based on the ID in table 1 equaling the position in table 2. Since the IDs in table 1 are sequential, the output appears to be sorted

change a table`s status to updated on inserting new values to another table in mysql

I've got two tables which are related to each other. I want to change table 1 status to updated when I insert new values into table 2 and return mysqli_affected_rows() for table 1
table 1: products
id | name
1 | product
table 1: categories
id | name | product_id
1 | cat 1 | 1
2 | cat 2 | 1
is this possible?!
You show the structure of the tables, but don't include any column that should be updated. This makes me think that you don't really want an update. You just want a query that returns whether a given product is in the categories table.
If so, the following may do what you want:
select p.*,
coalesce( (select 1 from categories c where c.productid = p.id limit 1), 0) as InCategories
from products p;
If you try to go another route where you actually store the value in the table, you will need to use a stored procedure or triggers. Do consider, though, what happens when you delete or update a row. insert is not the only way to modify data in a table.

How to search in serialized content

I have two tables
Table 1 is:
id | item_id
---------------
1 | a:2:{i:0;s:1:"1";i:1;s:1:"2";}
and the second table is:
tag_id | tag
------------
1 | c
2 | java
I have stored the id of table two in table 1 after serialize. Now I want to search the tag in table 1 by mysql %like% but the problem is that if I want to search the tag have id 1 or 2 in the item_id it will always find the result even if there is not 1 stored by me but this is stored by the serialize function. Now I want to know what should I do? And any better approach/method will also be appreciated!
Serialized data is not suitable for searching.
It's better to improve your table structure:
Table tags is the same as yours.
Table items_tags with stucture:
id - autoincrement field
item_id - ID of item with tag
tag_id - tag ID
With following structure it's much more easier to search items with tags.
Added
Mayankswami, example queries: For all items with specified tag: SELECT item_id FROM items_tags WHERE tag_id=(TAG_ID) For item's tags: SELECT * FROM item_tags AS IT LEFT JOIN tags AS T ON T.id=IT.tag_id WHERE item_id=(ITEM_ID)

using the value of mysql column to specify table name

Suppose I wanna do a join between table 1 and other tables...table 1 contains a column that specifies which table that row should be joined with
eg:
Table 1:
entry | tableName
333 | table3
4444 | table2
111 | table3
so 333 should be joined with table3, 4444 with table2, etc....
is there a way to specify mysql queries to use the column values like this as the name of the table to be joined with the entry?
You will have to fetch the result set from this table where you have the entry, table details & build a dynamic query. You will not be able to do this in a single query.
Not sure why you have such requirement. It's always best to know what are the tables & relationships to get the best possible join.
You can only do that if you build a dynamic query.

Categories