My MySQL field having skills field(string) like,
1)php,mysql
2)java
3)java,c
I need to select the column with skills field condition. Condition may be Like,
skills="php" // o/p => 1st column
or
skills = "mysql,c" // o/p => 1st and 3rd column
How I can write Query.? Here im using PHP.
One way to do it
SELECT *
FROM table1
WHERE skills LIKE '%mysql%'
OR skills LIKE '%c%'
SQLFiddle
It would be much easier if you'd normalized your data
Use a procedure like this one:
Split comma separated values from one column to 2 rows in the results. MySQL
To split your csv skills field into a temp table with rows for each skill
Then do the same thing with your search parameters (which I assume is also csv string passed in)
Then simply apply a JOIN over the two results and get the DISTICT Id.
there are many solutions for splitting a comma separated string in MySQL (though it's not particularly good at this task)... if you can't properly normalize the data then you can't very well complain about inefficient query routines.
Related
Essentially I have a table in my database called Table1 with the following data:
The table has a ProductID that repeats because the values of AssignedColour, ColourFinding and ColourPower vary.
I would like to present all ProductID data in one single row, meaning if there is more than one AssignedColour, ColourFinding and ColourPower listed, it will contain a number at the end.
The final result I of the SELECT query should look like the following:
The number of columns presented horizontally is based on the number of AssignedColour per ProductID
Is something like this possible to accomplish in a mySQL SELECT Query?
An SQL query cannot expand the number of columns of the result set depending on the data values it discovers during query execution. The columns in the SELECT-list must be fixed at the time the query is prepared, before it reads any data.
Also the column names cannot be changed during the query execution. They must be set at the time the query is prepared.
There's no way to do what you are describing in a single SQL query. Your options are:
Do two queries: one to enumerate the colors per product, and then use the result of the first to format a second query with the columns you want.
Do one query to fetch the data in rows as it exists in your table, then write code in your app to display it in rows however you think is best.
Either way, you have to write at least a bit of code in the client. You can't do this in one query.
I would like to ask how to filter a table by column with array value, I have a column "categories" that can have a value like [1,2,3] now I want to filter the table with that table like SELECT * FROM TABLE WHERE CATEGORIES = 2,
I also tried LIKE but it seems like it is not the proper way to do it, any opinions and suggestions would be appreciated.
It is not recommended way to store comma separated values in Column which needs to be filtered for queries.
You can use normalization and introduce relationship tables for categories and category_ids to be stored in different tables, then you can use join and where clause to achieve your results.
So i have a few numbers that i stored into row as (1,2,3) ( lets name it as "ids" in table "some" )
And i using LEFT JOIN in another query to get some row using this field as ids
But when i wrote
LEFT JOIN sometable AS st WHERE some2.id IN some.ids
i get an error.
How i can use it correct, or maybe another way to realize that
thanks
Storing comma-delimited lists of integers as a string is the wrong way to store things. You should be using a junction table.
If you are stuck with this format and don't care about performance, you can use find_in_set():
where find_in_set(some2.id, some.ids) > 0
Using an MS Access database (I know), the query below
SELECT * FROM PageImage WHERE (PageImage.Rooms) In ('1');
only returns rows when the 'PageImage.Rooms' column has a single value like 1
The actual data stored in the 'PageImage.Rooms' column is a comma separated list like 1,2,3 stored as a string, but no data is returned when this is the case.
From the above query & referring to the image below, only row 342 is returned, when I expect 341,342,343 to be returned
Given that PageImage.Rooms stores a genuine comma-delimited list as you specified in a comment (i.e. it is not an Access 2007+ multi-value field), you really have created a bit of a mess. A table containing such a column is for that reason completely un-normalized (not even in first normal form). Query and data consistency issues are to be expected.
The usual way to model a many-to-many relationship such as you have would be with a separate table. In this case, however, since the PageImage entity seems to have no data other than its Id, you could do it in the same table. Either way, whichever table models the relationship should have a separate row for each (PageImage.Id, Room) pair, and those pairs should be a composite key for the table.
With the structure you have now, the query you seem to want could be implemented like this:
SELECT * FROM PageImage
WHERE
PageImage.Rooms = '1'
OR PageImage.Rooms LIKE '*,1'
OR PageImage.Rooms LIKE '1,*'
OR PageImage.Rooms LIKE '*,1,*'
;
As Gord Thompson observed, that could be expressed more compactly, and perhaps more efficiently, as
SELECT * FROM PageImage
WHERE (',' & PageImage.Rooms & ',') LIKE '*,1,*'
;
As you can tell, it gets worse if you want to compare against multiple room numbers in the same query.
Bottom line: strings containing comma-delimited lists are just strings as far as the database is concerned.
I decided to scrap the multi-value string field & go down the correct route of creating a mappings table
The IN operation is the same action as performing an expanded OR statement where
SELECT * FROM PageImage WHERE (PageImage.Rooms) In ('1','2');
is the same as
SELECT * FROM PageImage WHERE PageImage.Rooms = '1' OR PageIMage.Rooms = '2';
Dealing with a partial match requires the LIKE keyword so SELECT * FROM PageImage WHERE PageImage.Rooms LIKE '*1*'; will return any field that has a one in any position within the field.
I have this data in a column
Steffi | ND Baumecker | Cassy
I would like to do a query to find if any of the above exist in another column
example of other column (Artist being column name)
Artist
Steffi
Derrick Carter
Ben Klock
Craig Richards
I don't think a LIKE will work here so wondering what query I can use to return the artist name from column 'Artist' when a match is made - so in the above example 'Steffi' would be returned.
Would I also need to remove the spaces before and after the | in the first column?
Thanks!
If I understand properly your problem: you want to filter rows using values from a column and searching for these values in another column?
SELECT a.first_name, a.last_name, a.nickname
FROM artist AS a
WHERE a.related_nickname IN (
SELECT sa.nickname
FROM artist AS sa
WHERE sa.popularity > 30
)
MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/any-in-some-subqueries.html
It seems that you are trying to achieve a complicated task and I'd advise you to try a couple of things.
Subqueries are useful but makes your queries much slower so using two queries might speed things up. The first query would pick the values that will be used for filtering and the second query will search rows.
If you filter by string, consider using indexes on your table: http://dev.mysql.com/doc/refman/5.1/en/create-index.html