MYSQL JOIN two tables - mysql

We just want to make the query for mysql database, in which there are 12 table according to the months(JAN - DEC), with 32 Columns(JAN1, JAN2, JAN3,....JAN31). These database is used for getting the availability for hotel,like if we select a tour for three days (29JAN-1JAN), so the query will check the records for 2 tables, one for JAN and other for FEB. the whole columns stored the values in digit(like, 5,10,2,0,5,etc)its showing Rooms available. We are successfully built a query for single month, but we unable to create a mysql query for 2 months, because we want a value in greater than 1. like we only shows the available rooms only.
$num = mysql_query("SELECT DISTINCT id,table_type,JAN29,room_type FROM JAN Where table_type='disp' AND JAN!=0 ");
above query is working fine for me, we just want this query for 2 tables. and getting the positive value , greater than 0(1).
Please help to solve this problem ..
Thanks
Rod
ID | JAN1 | JAN2 | JAN3 | JAN31|
34 | 5 | 3 | 3 | 4 |
56 | 4 | 3 | 9 | 3 |
28 | 0 | 7 | 0 | 9 |

Related

SQL query, check year on one column

I have a column in my MySQL table showing year.
Example:
Table: Information
ID | Year_sold
--- --------
1 | 2002-2010
2 | 2005-2015
3 | 2011-____
4 | 1975-1978
I will ask the table if it has data to show for a specific year.
Example:
SELECT * FROM Information WHERE Year_sold = '2012';
This will of course not work, but how can I type the SQL query if the result should be ID 2 and 3.
If the item is still active and being sold, the years will be shown like ID 3, "2011-____". I can replace the "____" if needed.
Use the BETWEEN, LEFT and SUBSTRING_INDEX functions.
SELECT ID, Year_sold
FROM Information
WHERE '2012' BETWEEN LEFT(Year_sold,4) AND SUBSTRING_INDEX(Year_sold,'-',-1)
Output
ID Year_sold
2 2005-2015
3 2011-____
SQL Fiddle: http://sqlfiddle.com/#!9/7df7b7/1/0
If possible I would start again with your table structure and have something like:
Table: Information
ID | Year_sold_from | Year_sold_to
--- ------------------------------
1 | 2002 | 2010
2 | 2005 | 2015
3 | 2011 | null
4 | 1975 | 1978

SQL - select x entries within a timespan

I'm creating a database (in MySQL) with a table of measurements. For each measurement I want to store the DateTime it came in. For showing plots within an app for different intervals (measurements of the day/week/month/year) I want sample the data points I have, so I can return e. g. 30 data points for the whole year as well as for the day/hour. This is the same as done with stock price graphs:
stock price plot for 1 day
vs
stock price plot for 1 month
As you can see, the amount of data points is the same in both pictures.
So how can I select x entries within a timespan in MySQL via SQL?
My data looks like this:
+====+====================+=============+==========+
| id | datetime | temperature | humidity |
+====+====================+=============+==========+
| 1 | 1-15-2016 00:30:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 2 | 1-15-2016 00:35:00 | 19 | 41 |
+----+--------------------+-------------+----------+
| 3 | 1-15-2016 00:40:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 4 | 1-15-2016 00:45:00 | 20 | 42 |
+----+--------------------+-------------+----------+
| 5 | 1-15-2016 00:50:00 | 21 | 42 |
+----+--------------------+-------------+----------+
| 6 | 1-15-2016 00:55:00 | 20 | 43 |
+----+--------------------+-------------+----------+
| 7 | 1-15-2016 01:00:00 | 21 | 43 |
+====+====================+=============+==========+
Let's say, I always want two data points (in reality a lot more). So for the last half hour I want the database to return data point 1 and 4, for the last ten minutes I want it to return 6 and 7.
Thanks for helping!
PS: I'm sorry for any errors in my English
OK, assuming a very simple systematic approach, you can get the first and last entry for any defined period:
select *
from table
where mydatetime =
(select
max(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
OR mydatetime =
(select
min(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
I believe your answer can be found at the following location:
https://stackoverflow.com/a/1891796/7176046
If you are looking to filter out any items not within your date/time your query would use:
Select * from table where Date/Time is (What you want to sort by)

How can I count entries for each day of the last 7 days in mysql? [duplicate]

This question already has an answer here:
MySQL group by date and convert from unix timestamp
(1 answer)
Closed 7 years ago.
I have a table in my database:
id | start_date | text
--------------------------
1 | 1446109949 | hi
2 | 1446109436 | gsdgsd
3 | 1446103439 | hfdhf
4 | 1446325345 | bvbcv
5 | 1446105754 | fdsfsd
6 | 1446266344 | fdsfds
7 | 1446643646 | hfdhfd
etc.
Basically it contains a lot of records, each of them contains its own timestamp.
I try to make a query that returns me the number of records for each of the last 7 days. So when user runs it today, it will show him that there are 3 texts assigned for yesterday, 5 for two days ago, and so on until 7 days ago.
Since I don't know too much about grouping in SQL, I assume this query will begin as SELECT Count(id) from USER_TEXT but I don't know how to proceed from that. Could you help me with that? Thanks.
If you want something like the following result :
date | num_texts
-------------------------
2015-10-28 | 3
2015-10-27 | 1
2015-10-26 | 8
etc.
then look at the query at MySQL group by date and convert from unix timestamp
However, if you would like the result to be something like :
days_ago | num_texts
-------------------------
0 | 6
1 | 3
2 | 1
3 | 8
etc.
then you can use the following query :
SELECT *
FROM (SELECT DATEDIFF(now(), FROM_UNIXTIME(start_date)) AS days_ago, COUNT(id) AS num_texts
FROM USER_TEXT
GROUP BY DATE(FROM_UNIXTIME(start_date))) AS temp
WHERE days_ago <= 7

mysql update with subquery 2 level deep

Thanks for taking a look at this question. I'm kind of lost and hope someone can help me. Below is a update query i would like to run.
This query now returns an error:
1054 - Unknown column 'spi.et_cross_rank' in 'where clause'
Some background:
from table: tmp_ranking_tbl
I would like to get the nth(spi.et_return_rank) record
for a group with value x (spi.et_cross_rank)
SET #rownum=0;
UPDATE STRToer_Poule_indeling spi
SET spi.team_id = (SELECT R.team_poule_id
FROM (SELECT #rownum:=#rownum+1 AS rownum, trt.team_poule_id
FROM tmp_ranking_tbl trt
WHERE trt.overal_rank = spi.et_cross_rank
ORDER BY trt.punten DESC, (trt.goals_voor - trt.goals_tegen) DESC, trt.goals_voor DESC) R
WHERE R.rownum = spi.et_return_rank)
WHERE spi.et_ronde = v_et_ronde
AND spi.poule_id IN (SELECT row_id FROM STRToer_Poules WHERE toernooi_onderdeel_id=v_onderdeel_id) ;
Data in tmp_ranking_tbl looks like:
team_poule_id | punten | goals_voor | goals_tegen | overal_rank
65 | 6 | 10 | 10 | 2
69 | 6 | 9 | 10 | 2
75 | 7 | 11 | 4 | 2
84 | 6 | 6 | 8 | 2
112 | 5 | 7 | 7 | 2
Thanks in advance for the help!
Update after question in comment about the goal, i'll try to keep it short. :-)
This query is used on a website to keep scores of a tournament. Sometimes you have an odd number of teams going to the next round. At that point I want to select the best number 3(spi.et_cross_rank) team across poules. This is setting saved in the STRToer_Poule_indeling with what rank per poule and the 1st, 2nd or nth team(spi.et_return_rank). The table tmp_ranking_tbl is filled with all rank 3 teams across the poules. When this if filled I would like the 1st or 2nd, depedining on the setting in STRToer_Poule_indeling, record to return.
Subset of structure the STRToer_Poule_indeling table
row_id | team_id | et_ronde | et_cross_rank | et_return_rank
1 | null | 1 | 3 | 1
Just check if you have a column named et_cross_rank on your table STRToer_Poule_indeling
The problem seems to be that SQL can't find that column on your table.
Hope it helps.

mysql query displaying incorrect rows

I have a query which currently should only display one row. However it somehow is displaying 4 rows as its result set even though 1) there are only three rows in the table to begin with 2) only one row matches the query criteria.
I am hoping someone might know what I am doing wrong with this MySql query
My database table structure is as below
smsid (int, auto increment), sms_type (text), sms_status (enum 'pending',sent'),
sms_error (test), sms_message(text), sms_mp3file (varchar 50),
sms_sendon (datetime), send_sms_toid (int 5)
My table entries are as so (following the order of the table columns above)
31 | mp3 | pending | | | helloworld.mp3 | 2013-11-20 16:16:00 | 7
30 | text | sent | | hello test | | 2013-11-18 13:12:00 | 8
29 | voice | sent | | testing 123 | | 2013-11-18 10:05:00 | 18
My query is as below
SELECT sms_messages.*, sms_recipients.cust_profid, sms_recipients.sms_cellnumber,
customer_smsnumbers.sms_number, customer_smsnumbers.sms_number
FROM sms_messages, sms_recipients, customer_smsnumbers
WHERE sms_messages.sms_type='mp3' AND sms_messages.sms_sendon <= '2013-11-21'
AND sms_messages.sms_status='pending' AND
sms_messages.send_sms_toid = sms_recipients.smsuser_id
In your query, you have missed a JOINING clause for customer_smsnumbers table. Similar to sms_messages.send_sms_toid = sms_recipients.smsuser_id you need to have a join clause which either connects sms_messages with customer_smsnumbers table or connects sms_recipients with the customer_smsnumbers table.
In the absence of a join clause other (unintended) records are included in the result.