I have some data in my database
---------
| Name | Offset(only for imagine it)
---------
| Aa | 1
| Ab | 2
| Ba | 3
| Cf | 4
| As | 5
---------
when use this query
SELECT * FROM table LIMIT 1,3;
it must be show 3 of the first data, Aa, Ab, Ba, with offset like that.
Now, I want to selecting all of Name with prefix a, so use this query
SELECT * FROM table WHERE Name LIKE 'a%' LIMIT 1,3;
I want to show the result of that query with the offset, how to do this?
Loop it is not the solution I needed.
---------
| Name | Offset(only for imagine it)
---------
| Aa | 1
| Ab | 2
| As | 5
---------
Any help appreciated
Thank you!
You have it mostly correct. However, rows start at 0. So the answer: SELECT * FROM table WHERE Name LIKE 'a%' LIMIT 0,3
That SQL query will select everything in the table table that starts with the letter 'a' (or 'A').
For alphabetical order, use ORDER BY Name.
Related
I have a table with three columns, id, name, and value, as shown below. I want to count the average where the id and value columns are the same, how can I do this?
+----+--------+-------+
| id | name | value |
+----+--------+-------+
| 2 | rahmat | 3 |
| 2 | olive | 5 |
| 3 | sari | 3 |
| 3 | ryan | 2 |
| 1 | zaki | 1 |
+----+--------+-------+
Try using this query:
SELECT AVG(value)
FROM table
WHERE id = value
The output from the sample table you gave in your OP would be 1.5, since sari and zaki are the only 2 users whose records have id and value columns which are equal.
according to your question yes you need to use
SELECT AVG(value)
FROM #table
WHERE id = value
I have created a sqlfiddle here
http://sqlfiddle.com/#!3/9eecb7/4105
from the nature of this question I feel you trying to calculate average of values of those rows having same ids. If that's the case I have created another fiddle http://sqlfiddle.com/#!3/9eecb7/4110 where you need to use group by
select id, sum(value)/count(id) as average from #table group by id
Lemme know if it is something you are after or you need something else.
My table User
+----+-----------+---------+--------+---------+------+-------+
| id | name | email | number | call_no | chek | Extra |
+----+-----------+-------+----------+---------+------+-------+
| 1 | one | a#a.com | 123 | 1164 | 1 | 1,2 |
+----+-----------+---------+---------+---------+------+------+
| 2 | two | a#a.com | 123 | 1164 | 1 | 2,1 |
+----+-----------+---------+---------+---------+------+------+
I have the field called Extra it contains the value 1,2
what is my question is ?
I need to match the 1,2 in the table id and myresult like one,two from the user table
For simple example query.what im tried is in below ?
select name from user Extra in('1,2');
My expected output is
one,two
If i tried to do in explode and if i run it in loop i can get the result but i need this in sql is it possible to get it?
You should have a separate table with one row per id and each value of extra. This is called a junction table. SQL has a great data structure for storing lists of things, called a table not a string. And, storing numbers as characters is even worse.
You can, however, do what you want with a join and aggregation:
select id, group_concat(e.name) as names
from table t left join
table e
on find_in_set(t.id, e.extra) > 0
group by t.id;
i have following table:
------------------------
| uid | attrvalue |
------------------------
| 1 | Spray |
| 2 | strong |
| 3 | very strong |
| 999 | Creme |
------------------------
now i have a query in php and like to find all rows where all queries are found.
SELECT * FROM attrtable WHERE MATCH (attrvalue) AGAINST ('Spray+very+strong' IN BOOLEAN MODE);
I like that it only finds row 1 and 3. But the resultrows are 1, 2 and 3.
So its important that it founds all words or word combinations in the table rows. The query doesn't contain words or combined words which aren't in the table (I check this first).
Just use LIKE, but the other way around to what your probably used to.
select query
from table1
where 'attrvalue' like concat(query,'%')
order by length(query) desc
limit 1
This is an example MYSQL result
+----+---+
| A | B |
+----+---+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 2 | 4 |
| 3 | 5 |
+----+---+
I would like to run through every distinct in Column A and do something utilizing the values in Column B.
Let's say A has userids and B has foods. I would like to grab all the foods that user 1 likes and then shoot an email to 1, then grab all the foods that user 2 likes and email to her, and so forth. Would appreciate any suggestions.
If you want comma separated values, you can use GROUP_CONCAT
SELECT A, GROUP_CONCAT(DISTINCT B) foodList
FROM tableName
GROUP BY A
SQLFiddle Demo
Other Link
GROUP BY clause
I have a table cities:
+----+--------+
| id | zip |
+----+--------+
| 1 | 07500 |
| 2 | 07501 |
| 3 | 75000 |
| 4 | 75001 |
| 5 | 75002 |
+----+--------+
And I need to select data where the field zip start with 750, so I've tried :
SELECT FROM cities WHERE ZIP LIKE '%750%'
But this returns me all data in the table that contain 750 and this isn't normal. How could I tell my query to select data that start with 750 ?
Do this query:
SELECT FROM cities WHERE ZIP LIKE '750%'
Explanation
% is a special MySQL character that means any character, empty string or group of characters.
Examples:
LIKE '%a%' -- matches: a, ant, track | any string that contains a in any location
LIKE 'a%' -- matches: a, ant | strings that start with a
LIKE '%a' -- matches: a, data | strings that end with a
This should make the magic:
SELECT FROM cities WHERE ZIP LIKE '750%'
% at begin means any thing before.
LIKE '750%'
should do the trick
if your string contains '%' character than you would need to use \%