Get out a mix from both tables in mysql [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 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

Related

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

Select only one number from array MySQL PHP [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 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

Get the place for the user entries [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 9 years ago.
Improve this question
I am really not good at math, but can someone help me though how i can get the place the user has on all entries? Etc. "You are now number 43 out of 20.403.044 entries"?
SELECT ?myPlace? FROM entries WHERE userid = 1 ORDER BY time ASC
ok, i think you want
SELECT COUNT(*) FROM TABLE WHERE id <= YOUR_USER_ID
You can get the rank with this query:
SELECT COUNT(*) + 1
FROM entries entries1
INNER JOIN entries entries2 ON (entries1.id != entries2.id AND entries2.time < entries1.time)
WHERE entries1.id = 4
You just need to count the number of people with a better time, and add 1 (since the first rank is 1).
Unfortunately MySQL lacks windowing functions. Therefore you have emulate them. One way to do it is something like this
SELECT userid,
(
SELECT 1 + COUNT(*)
FROM entries
WHERE time < e.time
) place
FROM entries e
WHERE userid = 1
Sample output:
| USERID | PLACE |
|--------|-------|
| 1 | 2 |
If you need total number of entries in the same query
SELECT userid,
(
SELECT 1 + COUNT(*)
FROM entries
WHERE time < e.time
) place,
(
SELECT COUNT(*)
FROM entries
) total
FROM entries e
WHERE userid = 1;
Sample output:
| USERID | PLACE | TOTAL |
|--------|-------|-------|
| 1 | 2 | 5 |
Here is SQLFiddle demo

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);

how to sum colum row by row [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
need to make table with asc and desc columns like below from length column.
+----------+-------+-------+-------+
| length | ordNr | asc | desc |
+----------+-------+-------+-------+
| 11 | 0 | 11 | 119 |
| 99 | 1 | 110 | 108 |
| 5 | 2 | 115 | 9 |
| 4 | 3 | 119 | 4 |
+----------+-------+-------+-------+
Can it be achieved in SQL? I know how to do it in php, but maybe javascript solution is easier?
this is how i done it in jsfiddle
I can't see how you can do this in SQL for two reasons
The values in the 'asc' column are dependent on the row order
The values in the 'desc' column can't be calculated until you have calculated the total of all the rows.
It would be much easier to read the 'length' column into an array in your programming language and handle all the necessary calculations within that array.
Try:
select max(t1._length) _length, t1._ordNr,
sum(case when t1._ordNr >=t2._ordNr then t2._length end) _asc,
sum(case when t1._ordNr <=t2._ordNr then t2._length end) _desc
from myTable t1 cross join myTable t2
group by t1._ordNr
(Underscores added to column names to avoid clashes with SQL reserved words.)

Categories