Get all row had value in column - php

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

Related

How to extract data from Column where the records are equal to 1?

I'll try to explain you the problem of mine. I'm searching for advice because I'm in impasse.
I have this database for an online store (it doesn't sell anything, it's just to show people owner's products).
I have two (they are more, but for this situation we'll consider just two of them) table. In the first:
products name | gender | id_product | etc..
john male 23
In the second:
id_product | man_56 | man_54 | man_52 | man_50 | etc...
23 0 1 0 1
id_product is the same as the first table. It's used to link the two table. Also 1=available, 0=not available).
Screenshot:
Now I don't know how to select/print only the column where records are 1 and not 0. Do I need to specify every column's name of the table?
You can use
SELECT * from tablename where columname = 1
tablename is your name of table i.e products
columnname is the name of feild which you want to check.
SELECT * FROM TableName WHERE 1 IN (ColumnOne,ColumnTwo,ColumnThree...)
For more info, check this link
I've solved my problem using multiple "if". Thanks to all of you :D

How to set one FK (foreign key) on several tables?

I have several Posts tables and one Votes table. How can I prevent inserting non-existing post_id (in Posts tables) in the Votes table?
// Posts_1 // Posts_2 // Posts_3
+----+---------+ +----+---------+ +----+---------+
| id | content | | id | content | | id | content |
+----+---------+ +----+---------+ +----+---------+
// Votes
+----+---------+
| id | post_id |
+----+---------+
It should be noted, in reality the structure of Posts tables is different. (all Posts tables have not the same structure), Then I can not combine all Posts tables as one table.
Now I want to prevent of inserting invalid rows in the Votes table. (invalid = post_id is not exist in the none of Posts tables)
So, If I have just one table, I can create a foreign key on the Votes.post_id reference to Posts.id, But the problem is having several Posts table. ok, well, Is there any suggest?
The table structure is crazy. You need to have a POST Index Table, which combines all the posts to one single place and gives it like this:
// Posts_Index
+----+---------+------------+
| id | post_id | post_table |
+----+---------+------------+
// Votes
+----+---------+
| id | post_id |
+----+---------+
Else you need to reverse map the way. So that, post_id -> votes.id.
A solution could be that you use post_table_ref in votes table to identify from which table the post has been voted for. This post_table_ref must be fixed for the whole application.
Depending on table from which post is coming from, you'll have to give post_table_ref in you votes update query.
E.g: if the post is coming from post_table_01 you'll have to set post_table_ref to 1 in votes update query. At the time of getting records from votes table you'll use the same post_table_ref in WHERE clause of your SELECT FROM VOTES query.
You can't in this data structure, because you don't have one primary key to reference, but three, which can have the same value multiple times in different tables.
Using a FK column for each posts_# table can have your Votes referencing no or multiple posts_# tables.
If you -really- need 3 or more different posts tables, you could create one central post_base table with PK and let the other posts tables reference it.
Table posts_base:
Id int primary key,
-- possibly other common data
then give the posts_# tables the posts_base.Id as FK and let Votes.post_id reference posts_base.Id!
Also, read some books about relational database design first; it's rather easy to create tables with somethin in it, but without knowing about foreign keys, normalization and the like, the database quickly becomes a messy trash dump, which can neither meet performance nor data integrity requirements.
Or, if you don't need it or want to code all that yourself (easier in the beginning than learning relational DB design, but troublesome later on), shift to NoSQL, where you just dump posts as documents and add Votes to these documents when submitted (so they can never be orphaned).

Select Query not work with where in

I have two table
one table is alldata ( here info_id is a text field data inserted using php )
=================
id | info_id
=================
1 | 2, 3, 5, 9
2 |
=================
second table is info
=================
id | name
=================
1 | one
2 | two
3 | three
4 | four
5 | five
6 | six
7 | seven
9 | eight
9 | nine
=================
now I want to select list of data from table two where data id will be matched with table one first item info_id data
my query is
SELECT i.* FROM `info` as i,`alldata` as a where i.id IN(a.info_id) and a.id=1
my query works but select only one item from table two.But there are multiple matched.
You have a very poor database design. First, storing numeric ids as strings is a bad idea -- numbers should be stored as numbers. Second, SQL offers this great data structure for storing lists. It is called a table, not a string.
You should really have a junction table, one one row per id and info_id.
That said, sometimes we a struck with substandard data structure. MySQL offers support for this. You can use:
SELECT i.*
FROM `info` i JOIN
`alldata` a
ON FIND_IN_SET(i.id, REPLACE(a.info_id, ', ', ',') ) > 0
WHERE a.id = 1;
You should also learn to use proper, explicit join syntax. If you use this method, instead of fixing the database design, you are not allowed to complain about performance. MySQL cannot take advantage of things like indexes to improve the performance of this type of query.

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