How to access only one record from my query - php

My Pastebin : http://pastebin.com/PmfDEdEw
Hi,
I am doing a bus search script and here is a table called "bus_notavailable" and it has fields as
nodate_id(PK)
bus_id(FK)
DD(INT 2)
MM(INT 2)
and YYYY(INT 4)
My Question is : Suppose Bus ID is 1 which is not available for these two dates are 15-08-2013 and 26-01-2013 so the record will filtered for this day...
The Nodates Table
Nodate_id bus_id DD MM YYYY
1 1 15 08 2013
2 1 26 01 2013
• so bus_notavailable.DD != '26' its showing me one record
• bus_notavailable.DD != '11' then its showing me 2 records
I know I did mistakes or don't know the solutions therefore want to find out the way to get rid of, and knew this is a issue of one to many relationship where the 'bus_notavailable' has two id of the same bus its showing me two records from 'bus' table.
So Do I need a sub-query to achieve this or is there any other methods?

Your condition here
LEFT JOIN bus_notavailable ON bus.bus_id = bus_notavailable.bus_id
.....
WHERE ......
bus_notavailable.bus_id IS NULL
AND bus_notavailable.DD != '$DD'
AND bus_notavailable.MM != '$MM'
AND bus_notavailable.YYYY != '$YYYY'
Is nonsensical because once bus_id IS NULL, there is no bus_notavailable record from which to compare the DD, MM or YYYY columns.
The condition you want is this (move the DD/MM/YYYY into the LEFT JOIN ON cluse):
LEFT JOIN bus_notavailable ON bus.bus_id = bus_notavailable.bus_id
AND bus_notavailable.DD = '$DD'
AND bus_notavailable.MM = '$MM'
AND bus_notavailable.YYYY = '$YYYY'
.....
WHERE ......
bus_notavailable.bus_id IS NULL

Man)))))) First of all you didn't say what you want to achieve. If you want to select buses that are available on day DD, then you are doing right.
The reason why second row returns 2 rows because as you say you select records where DD not equal to 11. And in this table, both records are suitable. That is why you get 2 rows.
If you want to select only one buss that is available for certain date in case there are many dates in this table you should use distinct keyword.
select distinct bus where dd!=11. You will get one row))
I mean only one row for each bus. So if you have 10 buses available on that date you will have 10 records. One record for each bus.

Related

Mysql UNION ALL to count multiple columns BUT also insert date column

Hi Guys I have a question. I am still learning and am trying to get some date out. Beneath is the table. It has hundreds of lines, but for example:
FormNR
Datum
XX1
XX2
XX3
0001
2022-09-08
4
23
7
0002
2022-09-10
8
5
0
The table name is 'forms'. Now what I need to do is to count XX1+XX2+XX3 (for a year rapport). Then I have a 'date from and to' selection box on my page. So the question would be:
What instanties have been used between a certain date in total but so that you can see a a total per Instantie (each number is a different instantie).
So for example...Between the 1st of January and the 1st of June a list of all XX numbers ( there are 36 ) with there total behind it
What I have is the following. Is works great and shows all XX's in a nice table but for the entire table, not per date. As soon as i want to add the 'between $date_from AND $date_to' it fails.
<?php
$sql_rg_total="SELECT forms.Datum, x.f1,Count(x.f1)
FROM
(SELECT XX1 As F1 FROM forms
UNION ALL
SELECT XX2 As F1 FROM forms
UNION ALL
SELECT XX3 As F1 FROM forms) x
WHERE x.f1 = '$subcat_id'
GROUP BY x.f1";
$resultvv=mysqli_query($conn, $sql_rg_total);
if (mysqli_num_rows($resultvv) > 0) {
while ($rowvv = mysqli_fetch_assoc($resultvv)) {
$subnr = $rowvv['Count(x.f1)'];
echo $subnr;
}
}
?>
By the way $subcat_id is from another table which connects the number to a name.
I have tried to write it as clear as I could. I know it's a bit thought haha. Thanks anyway for any input. Really stuck.
This query should do it:
SELECT SUM(x.c) AS c
FROM (
SELECT ((XX1 = '$subcat_id') + (XX2 = '$subcat_id') + (XX3 = '$subcat_id')) AS c
FROM forms
WHERE Datum BETWEEN '$date_from' AND '$date_to'
) x
The value of a boolean condition is 1 when it's true, 0 when it's false. So XX1 = '$subcat_id' + XX2 = '$subcat_id' + XX3 = '$subcat_id' adds up the number of columns that match in a row, then SUM(c) totals them in the entire table.
You don't need GROUP BY, since it's the same column that you're filtering in the WHERE condition (and now in the SELECT expression). And this moves the date condition into the subquery.

Search Two Tables & Concatenate Answer

I am trying to search two tables, match the results and then concatenate the answer... Only finding results >= today's date. This will then give the user the option to delete the selected from the DB. So...
Table 1 called Prog_name
id prog_name
1 Breakfast
2 Mid Morning
3 Afternoon
Table 2 called talk_ups
id date_tx prog_name (prog_name value = prog_name.id)
1 2017-06-30 2
2 2017-07-03 1
3 2017-07-01 3
The result I am after is something like: "01-07-2017, Afternoon". But I do also need the talk_ups.id to ensure it only deletes the correct record.
I managed to figure out how to get the name to match the talk_ups.prog_name value:
'$sql. = "SELECT talk_ups.prog_name, prog_name.id as progID, prog_name.prog_name as theName FROM prog_name, talk_ups WHERE talk_ups.prog_name = prog_name.id";'
But I can't figure out how to do the two searches and end up with the right result and how to separate out the results to then concatenate them.
You can use JOIN with WHERE condition, e.g.:
SELECT pn.id, pn.prog_name, tu.date_tx
FROM prog_name pn JOIN talk_ups tu ON pn.id = tu.prog_name
WHERE tu.date_tx > NOW();

How to filter the total sales record every month using sql query?

I am generating a sales report and I am having a hard time with the query. I am not really good in query. I hope you can help me with this. Ok here it is.
I have a sales table. And the structure is like this
EX:
product_id name date_purchased
1 apple 2014-01-03 12:00:59
2 orange 2014-01-05 10:12:20
3 banana 2014-02-01 09:25:01
4 mango 2014-02-20 18:13:25
5 stawberry 2014-02-28 13:14:30
6 jackfruit 2014-05-26 08:16:31
7 grapes 2014-11-03 09:25:21
8 guava 2014-12-25 10:15:45
Now I want to count the item purchased every month
My output result should be like this
2,3,0,0,1,0,0,0,0,0,1,1
2 represents Jan,
3 represents Feb,
etc...
My query looks like this but I know it is wrong.. :(
SELECT COUNT(product_id) FROM sales_table GROUP BY date_purchased
How can I do that using a query? Or should I make a PHP function for this?
try like this :
SELECT year(date_purchased), month(date_purchased), COUNT(product_id)
FROM sales_table
GROUP BY concat(year(date_purchased), month(date_purchased))
I think you can retrieve the dates and use PHP as it is pretty straight forward.
Once you have a date lets say for example,
$date_str = "2014-01-03 12:00:59"; //let's say you fetch this from DB
$month = date('m',strtotime($date_str)); //this will automatically output 1
You mentioned, 2 represents Jan, 3 represents Feb etc, so you can just add 1 to $month and you have what you need.
Hope it should work
SELECT COUNT(product_id) FROM sales_table GROUP BY DATENAME(month, date_purchased)
The SQL query is enough.
You have to use group by.
But first make a month column for your table.
create function byMonth(#myDate datetime)
returns datetime
as
begin
declare #newDate
set #newDate = year(#myDate) + '/' + month(#myDate) + '/1 00:00:00'
return #newDate
end
go
and use this in your query:
Select Count(*),byMonth(date_puchased) as x
from myTable
where ...
group by x
having ...
this should work for you.
More elaborated solutions
To get Month - wise
SELECT COUNT(id) FROM testrec GROUP BY DATE_FORMAT(pur,'%M');
Get all details
SELECT COUNT(product_id),`name` , date_purchased, DATE_FORMAT(date_purchased,'%M') FROM sales_table GROUP BY DATE_FORMAT(pur,'%M');

cant get maximum date in listing users php MYSQL

I have two tables, and I want to get the last enterd date.
The first table is seeker:
seeker_nic-----username
111-------------ali
222-------------umer
333-------------raza
The second one is requestblood:
id-------seeker_nic-----requireddate
1------- 111 ----------2012/10/9
2 ------- 222-----------2012/5/8
3 ------ 111-----------2012/12/12
4 ------- 111-----------2012/11/12
5---------111-----------2012/09/09
6 ------- 222-----------2012/7/9
7 ------- 333 ----------2012/4/4
Now, I want to list the users with their last inserted date like..
s.no---- username----- requireddate
1------- ali---------- 2012/09/09
2------- umer--------- 2012/7/9
3------- raza--------- 2012/4/4
i am using this query
SELECT seeker.username,bloodrequest.requireddate,
max( bloodrequest.bloodrequest_id ) AS maxdate
FROM seeker
JOIN bloodrequest ON seeker.seeker_nic = bloodrequest.seeker_nic
GROUP BY seeker.username
when i run this query in phpmyadmin.. its lists all user and show the first inserted date of each user.. but i want the last inserted date ... what should i do.. please i need help :(
EDIT for real answer:
Not very clean sql, but it will get you the results you are looking for. I'm sure there's a way do it better with group by, but this works. :)
SELECT s.username,
(
SELECT br1.requireddate
from bloodrequest as br1
where br1.bloodrequest_id =
(
select max(br2.bloodrequest_id)
from bloodrequest as br2
where br2.seeker_nic = s.seeker_nic
)
) as requireddate
FROM seeker as s

Need to see if a range of dates overlaps another range of dates in sql

I have a table which stores bookings of rooms, the schema is:
ID | ROOM_ID | CHECK_IN_DATE | CHECK_OUT_DATE | USER_ID
I need to run a search query for rooms which are available/unavailable between a set range of dates.
Also keep in mind that there exists another table which holds dates when the room is prebooked and its in the format:
ROOM_ID | DATE
SO I need to run a query which looks for rooms available within a set range, How would I formulate the query?
I'm using MySQL here.
---edit---
Theres also a Rooms table of the schema:
ID | ROOM DETAILS etc
The unavailability/prebooked dates table basically holds sporadic single dates, each date in the unavailability table refers to a date when the room for some reason cannot be booked eg: maintenance etc
SELECT
ROOM_ID
FROM
Rooms r
LEFT JOIN Bookings b ON (
r.ROOM_ID = b.ROOM_ID
AND b.CHECK_IN_DATE > '$MAX_DATE'
AND b.CHECK_OUT_DATE < '$MIN_DATE'
)
I'm not sure how pre-booked rooms factors in as there is no date range. Do pre-booked rooms also get an entry on bookings or not?
SELECT id FROM rooms WHERE id NOT IN (
SELECT room_id FROM bookings
WHERE check_in_date < 'end date' AND check_out_date > 'start date'
);
there are like 5 possible ways:
---s-----e---
s-e----------
--s-e--------
-----s-e-e---
--------s-e--
----------s-e
s = start / e=end firste line is your database set and the other are possible searches
only the first and last one are the ones that you want so what you look for is:
search.end < entry.start OR search.start > entry.end

Categories