have a string($string) and a Column(DOMAIN) in a table like this:
$string="'domain3','domain2,'domain1'";
-----------------
| DOMAIN |
|---------------|
| domain1 |
| domain2 |
|domain1,domain2|
-----------------
And I am trying to create a sql query that return a result if the domain is included in the string. What I found is that I have to split the entries in the column, so I have something like this:
SELECT * FROM TABLE1 WHERE SUBSTRING_INDEX(DOMAIN, ',', 1) IN ('$string')
But this gives me a result only if the first index match, I want to do a kind of loop.
I don't know if my question is clear enough, but to explain the context, I am trying to filter results using checkboxes.
Thanks!
What you get as results is right. You only substring from the 1st index. What you are looking for an explode functionality in mysql.
You can read the comments on this page for a plethora of solutions to the string-splitting problem: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html.
Related
I am trying to handle as much as possible inside the query since this is the fastest way of listing things in my current project.
Here's what I'm trying to do.
I have a table with stations:
id | station_call | station_band
--------+-----------------+-------------
1 | WABC | FM
2 | WXYZ | AM
Now normally, upon getting the resutls, it would be easy to just join the two with PHP to get the full station name
$row["station_call"] . "-" . $row["station_band"];
would result in WACB-FM and WXYZ-AM
Is there a way I can get this joining of the two inside the query?
Basically returning a new row, something like station_name and have the name already formated as WACB-FM
Bonus:
This probably makes it a bit harder, my query is also getting these results inside of a JOIN statement and processed as a GROUP_CONCAT()
Right now, I have two get separate GROUP_CONCATS() to return as two separate columns in each row resulting in "WABC, WXYZ" and "FM, AM" and having to explode the strings and join them based on index
Basically, I need it to be returned as a series of station names separated as a comma.
So when I get the final row, I'm trying to just reference $row["stations"] and get "WABC-FM, WXYZ-AM"
SELECT concat(station_call, '-', station_band) AS station_info FROM stations;
You need the mysql-concat function.
The Solution within a group_concat-functions (Bonus-Part of question) looks like this:
SELECT GROUP_CONCAT(CONCAT(s.radiostation_call, '-', s.radiostation_band) SEPARATOR '|') AS station_info FROM stations GROUP BY ID
Is it possible for Mysql to return the search value that it used to find the row?
For example I have a database like this:
ID | FLIPPERID | PHONENUMBERS | POSTERIDS
1 10001 7003158974,8769873453,9085699812 6477741332,34234324234,5734345,34234
I do a query like this. But with hundreds of values:
SELECT * FROM `flipperaccounts`
WHERE `posterids` = 3126764
OR `posterids` = 65139757
OR `phonenumbers` = 6477741332
OR `posterids` = 72345341;
Now I'm wondering if there's a way to know which value triggered the row to showup?
You can do a direct comparison using a case/when/then statement. I've used CONCAT_WS here in the event that there are multiple columns matched, so you can explode(',', $row['matched_column']) for your convenience.
SELECT
ID,FLIPPERID,PHONENUMBERS,POSTERIDS, CONCAT_WS(', ',
CASE WHEN posterids = 3126764 THEN 'posterids_1',
CASE WHEN posterids = 65139757 THEN 'posterids_2',
CASE WHEN phonenumbers = 6477741332 THEN 'phonenumbers',
CASE WHEN posterids = 72345341 THEN 'posterids_3' ) AS matched_column
FROM flipperaccounts
Then when you get the result back your table will be like:
| ID | FLIPPERID | PHONENUMBERS | POSTERIDS | MATCHED_COLUMN |
"1" "10001" "700315894.." "647774..", "posterids_2"
Then you can access it within your loop statement as an index on the row.
if(!empty($row['matched_column'])) {
echo $row['matched_column'];
}
Hopefully this helps.
I do not know of any way to tell what part of a complex WHERE clause caused a SELECTion.
Note that OR is associative (rather than commutative), so more than one of your clauses may cause a hit, and so the order is important to your answer — problem is, the query optimizer knows that OR is associative, and so it may arbitrarily change the order of the OR clauses! For example, if one column has a shorter index, the query optimizer might choose to do that first, or it may choose to do PRIMARY or UNIQUE columns first.
So it seems to me that your question in non-deterministic, without returning an array of OR clauses that "hit."
You could do this by using "SELECT COUNT(*)" for each of the OR clauses, which is fairly non-intensive, if the columns are indexed.
I went with RiggsFolly idea of making a child table. Thanks for the help.
About
I have this table in my database holding some information saved with a user id and time.
| CONTENT | USER ID | TIME |
| text | 1 | 1405085592 |
| hello | 2 | 1405085683 |
| hey | 1 | 1405086953 |
This example could be a data dump from my database, now as you can count there is "three" rows. However I only need to know how many users there have some information in my database. Therefor the result I'm really looking for is "two", because only two users have information in the database. User ID 1 is owning both "text"(1) & "hey"(3) where user ID 2 haves "hello"(2).
In short
I want to count how many users (regardless how many rows of information they have) there are inside my database.
** What I tried **
I tried to fetch every single row into an array and then using array_unique to count them together, works fine but I do not see this as a clean and best way to do this.
Then what?
I could use the array_unique and just use count to see how many rows there are, but I'm looking for something more clean. I tried to search for this, but I'm not actually sure what I should search for in term to hit something I'm looking for. After being stuck and though I wanted to learn something new, I wanted to post this problem here.
Note
I hope you guys can help me, I have tried to make it clear what I'm looking for and what I tried. If not please let me know. Sorry if some of the above contains misspelled words, incorrect grammar or is badly explained. I do not speak English daily, but I try my best.
You are looking for the DISTINCT keyword. It returns the count of unique values of a column:
SELECT COUNT(DISTINCT user_id)
FROM your_table
See example on SQL Fiddle.
This query:
SELECT DISTINCT user_id FROM table
will return just one row for every user in the table.
I have sample data in database with multiple different name,date and menu... I need to proceed data with the same name and date, but each data has a different menu...
DATABASE
the problem is that I need to store all the menu from the same name and date as a new variable since I need to call it for table view in web, and the table need to be look like this :
TABLE
I already using array to store the data menu for each same name and date, so the menu come out like this :
I'm really confused at how to continue make approach after this part,
How do I get output --> Candy Cake Cheese , so I can call it in table (?)
I think I can't seem to just use concat_ws in my query, since the values are from the same field of array, or is there another solution so I can get the output table I need ??
Thanks :)
You're probably looking for MySQL GROUP_CONCAT() aggregate function:
SELECT Name, Date, GROUP_CONCAT(Menu) AS Menu
FROM Table
GROUP BY Name, Date
This will give your data like that:
|--------|------------|-------------------|
| Name | Date | Menu |
|--------|------------|-------------------|
| Sylvie | 2001-01-01 | Candy,Cake,Cheese |
| Sylvie | 2001-02-01 | Milk,Tea |
|--------|------------|-------------------|
From that you just need to use PHP explode() function to get each menu record as an array.
Use serialize or json_encode to store the value into the database and use unserialize or json_decode when you get the value from the database.
I'm trying to write a query in php that will select a row from a database that partially matches a string.
Better explained via an example:
Say my DB has a field called numberString, with value 'abcd'. I want to write a query that, given "123abcd", will return a field of that row of the database.
SELECT mood
FROM USERS
WHERE numberString like '%$giveNumberString'"
$giveNumberString is the functions parameter (i.e. the string that I want to look for)
But I think those two numberString and '%$givenNumberString'" should be like the other way around in order for the query to work as expected. Is there a way to do this?
Ok the table looks like this:
id | username | numberString | mood
-------------------------------------
1 | myUsrNam | abcd | happy
Now I want, given "123abcd", to retrieve the mood of that person. In other words to match 123abcd against abcd.
I'm not sure this is a legal syntax, but let me try a wild guess: have you tried
SELECT mood from users where '$giveNumberString' like '%' || numberString || '%'
?