Select only one number from array MySQL PHP [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i have the following query bellow
SELECT * FROM subcat where catid in (2) order by id desc
what i am trying to do is select one number only in catid i have ex:
1,2,3,4,5
this work but only when "catid" start with number "2" then have ","
example
ID | 1
subcat_name | PHP
catid | 2,3,4
ID | 2
subcat_name | ASP
catid | 5,2,3
ID | 3
subcat_name | MYSQL
catid | 6
ID | 4
subcat_name | C++
catid | 2
i want to select the subcat with only catid = 2

Try this
SELECT * FROM subcat where FIND_IN_SET(2,catid) order by id desc

Related

How to search in MySQL exploded array data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
My table structure as fallows
+----+------+---------+---------+
| id | name | heading | catid |
+----+------+---------+---------+
| 1 | ajay | xyz | 1:25:22 |
| 2 |sanjay| abc |15:25:45 |
+----+------+---------+---------+
If i get condition catid=22 then get result
+---+-----+----+---------+
| 1 | ajay| xyz| 1:25:22 |
+---+-----+----+---------+
If i get condition catid=15 then get result
+---+-----+----+----------+
| 2 | sanjay| abc|15:25:45|
+---+-----+----+----------+
If i get condition catid=25 then get result
+---+-----+----+----------+
| 1 | ajay| xyz| 1:25:22 |
+---+-----+----+----------+
| 2 | sanjay| abc|15:25:45|
+---+-----+----+----------+
You could use FIND_IN_SET, after replacing the colons in catid with commas:
SELECT *
FROM yourTable
WHERE FIND_IN_SET('25', REPLACE(catid, ':', ',')) > 0;
But a good long term investment would be to normalize the catid data and get those IDs in separate records.
There is also a way to do this using the LIKE operator, but it is ugly:
SELECT *
FROM yourTable
WHERE CONCAT(':', catid, ':') LIKE '%:25:%';

SQL query using data from other table [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a normalized table:
`Table: TheMovies`
id | MovieName
---------------------
1 | Zootopia
2 | Moana
3 | Toy Story
`Table: TheGenres`
id | GenreName
---------------------
21 | Action
22 | Animation
23 | Adventure
`Table: mMoviesGenres`
movieID | genreID
---------------------
1 | 21
1 | 23
2 | 22
2 | 23
2 | 21
3 | 23
All works fine, but I need a Query which will shoe me similiar movies based on at least one of the genres of MovieID = 1.
Can you give me an sql query so I have a basic idea of doing that, to be able to create more advanced queries?
To query using data from another table, you can join two or more tables into a single table by using JOIN clause.
SELECT TheMovies.* FROM mMoviesGenres JOIN TheMovies ON mMoviesGenres.MovieID = TheMovies.MovieID WHERE mMoviesGenres.MovieId <> 1 AND mMoviesGenres.GenreID IN (SELECT GenreID FROM mMoviesGenres WHERE MovieID = 1)
Learn more about join: Using Join to Retrieve Data from Multiple Tables
SELECT TheMovies.*
FROM mMoviesGenres
JOIN TheMovies
ON mMoviesGenres.MovieID = TheMovies.MovieID
WHERE
mMoviesGenres.MovieId <> 1
AND mMoviesGenres.GenreID IN (SELECT GenreID FROM mMoviesGenres WHERE MovieID = 1)

MySQL - Retrieve data from database by joining 3 tables and display one column value with comma [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have three tables, named location and Hospital and hospital location. These are the fields and data of both table
Table : location
id | location_name
1 | location1
2 | location2
Table : hospital
id | hospital_name
1 | Hospital1
2 | Hospital2
Table : hospital_location
id | hospital_id | location_id
1 | 1 | 1
2 | 1 | 2
I need to create a query in mysql to display all the data from hospital table. The location_name column has multiple values, separated by a comma.
id | hospital_name | location_name
1 | Hospital1 | location1, location2
What you need is the GROUP_CONCAT mysql function
http://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html#function_group-concat
SELECT h.hospital_name, GROUP_CONCAT(l.location_name) as location_name
FROM hospital h
LEFT JOIN hospital_location hl ON hl.hospital_id = h.id
LEFT JOIN location.l ON hl.location_id = l.id
GROUP BY h.id

Get out a mix from both tables in mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How can i get out from mysql ORDER BY date from two tables with limit 4 ? I want to get out a mix from both tables ORDER BY date >= DATE(NOW()).
tbl1
id place1 date1
1 | example | 2013-01-05
2 | example | 2013-07-05
3 | example | 2013-23-05
tbl2
id place2 date2
1 | example | 2013-05-05
2 | example | 2013-06-05
3 | example | 2013-20-05
SELECT *
FROM (SELECT id, place1 place, date1 date
FROM tbl1
WHERE date1 > CURDATE()
UNION
SELECT id, place2 place, date2 date
FROM tbl2
WHERE date2 > CURDATE()) tbl12
ORDER BY date DESC
LIMIT 4

select an entry with specified id, and also one entry before and after with the same foreign key value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a table which looks like this one:
| id | fk_book | name |
-----------------------
| 1 | 2 | test1|
| 3 | 2 | test3|
| 6 | 3 | notes|
| 7 | 2 | test2|
No I want to get the entry with the id 3. select * from test where id=3 AND fk_book=1;
but is there also a way to get the item with id 1 and 7?
and I dont know the ids of the other entries
thanks
I think you need this:
select * from test where id=3 AND fk_book = 2
union all
select * from test where id < 3 AND fk_book = 2 order by id desc limit 1
union all
select * from test where id > 3 AND fk_book = 2 order by id asc limit 1
Try following sql query:
select * from test where id in(1,3,7);

Categories