Mysql how to select row where field's array contains this value - php

i have this table structure "posts"
+----+-------+---------------+
| id | post | authors_ids |
+----+-------+---------------+
| 1 | test1 | ["1","2"] |
+----+-------+---------------+
| 2 | test2 | ["1","3"] |
+----+-------+---------------+
| 3 | test3 | ["3","4","5"] |
+----+-------+---------------+
what i want to do is to select rows that contains in authors_ids field a particular value for example i need to select the posts where authors_ids array contains number 4
i know that the table structure is wrong but at this time i will not be able to change it.
how can i achieve that ?

One quick way to achieve this would be to select using LIKE:
SELECT * FROM `posts` WHERE `authors_ids` LIKE '%"4"%';

Related

sort results from database by cell value

i have database like this
============================
| id | name | value | key |
============================
| 1 | sara | | 1 |
============================
| 2 | sara | | 1 |
============================
| 3 | sara | 1 | |
============================
| 4 | jhon | | 1 |
============================
| 4 | jhon | 1 | |
============================
i want first to get only one result for each name
my expected output
jhon
sara
i use
select * from my_table
but it's display all names
and need to sort table by key cell
my expected output with sort
sara (3 keys)
jhon (1 key)
The function you are searching is called group by.
SELECT name, SUM(key) FROM my_table GROUP BY name
You can also use other aggregate function (not only SUM), maybe you want
SELECT name, COUNT(*) FROM my_table GROUP BY name
SELECT name, COUNT(key) FROM my_table GROUP BY name
Search for some examples for group by (also here on Stack Overflow) and check out the different results.
The other function you are searching for is called order by.
Please read some book or tutorials about sql, this is pretty basic stuff.

MySQL query to return unique values in one column and sorted by id to get a PHP array

I have the following simplified table: (Note we skipped 2nd exam in the exam_id)
+----+---------+-------+
| id | exam_id | score |
+----+---------+-------+
| 1 | 1 | 15 |
| 2 | 1 | 20 |
| 3 | 1 | 68 |
| 4 | 3 | 92 |
| 5 | 3 | 10 |
+----+---------+-------+
I want to write an $sql and some php (I'm using Wordpress, and can use $wpdb) to be able to get the following:
$exam[3]=10
$exam[1]=68
Not that when there are multiple exams, we take the score entry which corresponds to the largest id
And $exam[2] is empty. In words, I'd like to save the last ever exam that the user attempted and show their score.
I've tried using Group By and
Try this
SELECT
*
FROM exams
WHERE exams.id IN (
SELECT
MAX(id)
FROM exams
GROUP BY exam_id
);

Using Two Tables in PHP & MySQL to display one loop result

Ok so a bit of a funny question here. I have two tables: -
LiveTable
ArchiveTable
The data is as follows: -
Table: LiveTable Table: ArchiveTable
| ID | NAME | | ID | NAME |
------------------ ------------------
| 1 | Test One | | 4 | Test Four |
------------------ ------------------
| 2 | Test Two | | 5 | Test Five |
------------------ ------------------
| 3 | Test Three| | 6 | Test Six |
What I want to do is merge them into one table for querying purposes only. Not as a Database Structure.
In essence when I do a PHP Loop I want the results to work like this: -
Merged Results
| ID | NAME |
------------------
| 1 | Test One |
------------------
| 2 | Test Two |
------------------
| 3 | Test Three|
------------------
| 4 | Test Four |
------------------
| 5 | Test Five |
------------------
| 6 | Test Six |
How would I go about doing this? Also is there a way of doing this with Doctrine?
You can use an SQL query with UNION:
SELECT ID, Name
FROM LiveTable
UNION ALL
SELECT ID, Name
FROM ArchiveTable
Note: UNION ALL will retain duplicates. If you want to remove duplicate records, then use UNION.
Yould use UNION:
SELECT id, name FROM tbl1
UNION ALL
SELECT id, name FROM tbl2
Create DB view using above two tables
CREATE VIEW view_name AS SELECT
id, name FROM tbl1
UNION
id, name FROM tbl2
Then you can query on your view
you can use the sql query multi table select
Select * from LiveTable,ArchiveTable
that's it

how to select data that have the same value with sql?

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.

How to construct table with dynamic input fields?

I have a PHP script where you can (as admin) select how many input-fields there will be in a question form. Some of the fields are non optional, but som are (as many as you like).
The table in MySQL for collecting the answers looks like this:
id | userid | fname | ename | seat | optional
If the admin want it to be two optional input-fields then the result of one filled form would take three tows in the table:
| 5 | 3 | Peter | Pan | 4 | |
| | 3 | | | | opt.value1 |
| | 3 | | | | opt.value2 |
Is this really the best way to store this in? How would you solve it?
And also, how can I make shure that the userid is unique for the user? I can't use the auto-increment key value thing in MySQL because the same value is on three rows...
The way i learned it you have to use multiple tables. Like this:
Table1:
id | userid | fname | ename | seat
Table2:
userid | optional
Table2.userid is a reference to Table1.userid
Then the fields that has to be filed can be put into the first table and all the optional in the second.
If i follow your example your database should look like this:
Table1:
id | userid | fname | ename | seat
5 | 3 | Peter | Pan | 4
Table2:
userid | optional
3 | opt.value1
3 | opt.value2
By the way, why do you have both id and userid in Table1?
Best practice would be to store "id" and "optional" values in a separate table.
Then pull the information you want from it for each "id".

Categories