For example, i have two tables like this
Table User:
Table Payment:
I want to join two table above, but i want to get the result as quantity,
How many of blanks price columns based from previous of current date.
So if current date is 2015-04-10, the result will be look like this:
I don't have any clue how to do that using some syntax query combination in SQL or in PHP to display the result
In SQL, you could do something like this:
SELECT user_name, COUNT(name) AS result
FROM user
JOIN payment ON users.id = payment.id_user
WHERE (price IS NULL OR ltrim(price) = '')
AND payments.last_pay < '2015-04-10'
GROUP BY payment.id_user
You can see the result of this query in the SQL Fiddle.
Related
I am trying to make PHP / MySQL Search Form. When a user wants to search he need to fill 3 input fields: FromDate, ToDate and SucategoryID.
There are 2 tables: Items and ItemsBlockDates
In Table Items, there is info about the item and it is identified by ID.
In Table ItemBlockDates, there is info about a period when Item will not be displayed in search results. It contains: ItemID, FromDate, and Date fields. Also, there is a possibility to define more than one Block Date for one item.
Now, I want to select all Items where FromDate and To Date inputs does not match with any of rows in ItemsBlockDates with same ItemID.
I wrote a query, but there is a problem:
SELECT *
FROM Items
LEFT JOIN ItemBlockDates ON Items.ID = ItemBlockDates.ItemID
WHERE Items.SubcategoryID = :SubcategoryID
AND ItemBlockDates.FromDate NOT BETWEEN CAST(:FromDate AS DATETIME) AND CAST(:ToDate AS DATETIME)
When I run this query it does not display Items where Block Dates are not set, also it displays items where more than one Block Date is set.
I want to select all Items where FromDate and To Date inputs does not
match with any of rows in ItemsBlockDates with same ItemID.
To me, this suggests NOT EXISTS:
SELECT i.*
FROM Items i
WHERE i.SubcategoryID = :SubcategoryID AND
NOT EXISTS (SELECT 1
FROM ItemBlockDates ibd
WHERE i.ID = ibd.ItemID AND
ibd.FromDate BETWEEN CAST(:FromDate AS DATETIME) AND CAST(:ToDate AS DATETIME)
);
If you are passing in a parameter, I'm not sure why you need to convert to a DATETIME. You should be able to pass it in with the correct type.
I want to display the logs to recent activities page ordered by date. Now I was trying to execute this to my mysql
"SELECT * FROM tracking_log.editlog, tracking_log.deletelog, tracking_log.loginlog, tracking_log.logoutlog ORDER BY time ASC";
but it always says
Column 'time' in order clause is ambiguous
all of the tables have a time column, format by datetime (0000-00-00 00:00:00)
How am I going to fetch them ordered by time?
Thanks in advance!
By which table's time column you want to order?
Assuming you want to order the result set by tracking_log.editlog.time column then the query would look like below:
SELECT
*
FROM tracking_log.editlog, tracking_log.deletelog,
tracking_log.loginlog, tracking_log.logoutlog
ORDER BY tracking_log.editlog.time ASC;
Just in case if all of the time columns in the respective table don't contain NOT NULL values at the same time then you need to use COALESCE I guess.
Query using COALESCE
SELECT
*
FROM tracking_log.editlog, tracking_log.deletelog,
tracking_log.loginlog, tracking_log.logoutlog
ORDER BY
COALESCE(tracking_log.editlog.time , tracking_log.deletelog.time, tracking_log.loginlog.time,tracking_log.logoutlog.time) ASC;
'tracking_log' is your database name, and you're selecting multiple tables from that database, so you need to specify from which table you want to order 'time' by:
select * from tracking_log.editlog, tracking_log.deletelog ORDER BY tracking_log.editlog.time ASC
or whichever table from your database you want to use 'time' from. This will fix the error but won't return any results because you have multiple tables in a SELECT clause without anything relating them together.
You'll need to specify some common columns on which you want to return results rather than getting the wildcard and then UNION the tables to aggregate the results. For example, if you have common columns userID, description and time in all your tables, you could do the following:
(SELECT userID, description, time FROM tracking_log.editlog)
UNION
(SELECT userID, description, time FROM tracking_log.deletelog)
ORDER BY time
I need to retrieve values that doesn't exist in another table. I'm working with Shares and Share_types tables.
Currently, I have this working with PHP but I'm always looping all over the 2,500~ rows of Share_types and I think it is possible to solve the amount of rows with a query.
The user currently goes through this process:
Select the type of share - Anual share
Select the year that will be extracted - 2016
Code will generate all shares until the year of 2016 that weren't yet generated. This means, that the years behind of 2016 will also be generate if they don't exist.
That said, my PHP code is like the following:
// Retrieves always the 2,500~ rows
$listOfShareTypes = "SELECT user_id, date_start FROM share_types WHERE type = 'Anual share'";
foreach($listOfShareTypes as $type)
{
// Now is where I validate if the share already exists
$hasShare = "SELECT COUNT(*) FROM shares WHERE the_year = $yearSelectedByUser, user_id = $type->user_id, share_type_id = $type->id";
if($hasShare == TRUE)
continue;
else
// Create the share..
}
So usually, to retrieve via query the results that doesn't exist in another table I would do two select in a query, but after a few searches it points to the use of LEFT JOIN. However I have no clue how to accomplish this since I need to match several fields (user_id, share_type_id, year, etc.)
Looking at this example I created on SQLFiddle, the result should be:
(5, 3, 'Anual', '2015-06-28')
And with this result and since the user selected the year of 2016 I should loop (in PHP) from 2015 until 2016.
You were using the wrong column in the join condition. The tables should be joined on user_id.
SQL Fiddle
SELECT stype.id, stype.user_id, stype.type, stype.date_start
FROM share_types AS stype
LEFT JOIN shares AS share ON share.user_id = stype.user_id
WHERE share.share_type_id IS NULL
try this query
SELECT user_id, date_start FROM share_types st,shares sh on sh.share_type_id=st.user_id WHERE type = 'Anual share' and sh.user_id=st.id and sh.the_year=$yearSelectedByUser and Having COUNT(sh.user_id) < 1;
How can i query and return all the rows but with "SUM" added to my query?
Here is my sample query:
select SUM(amount) as total, user_id, username, from table where user_cat = '1';
if I remove SUM, I get the correct number of rows returned, but when I add SUM, the row result is decreased.
Any Idea how to do this?
You have noticed it's impossible to make the query in the way you tried as SUM() aggregates the other rows and only one row is displayed. One way to solve this is to separate SUM() in a subquery as described below:
select n.total,
user_id,
username
from table,
(select SUM(amount) as total
from table
where user_cat = '1') n
where user_cat = '1';
I prefer to make this in two separate queries. Now you'll have to deal with the first column and make it invisible. If you use two queries you'll have to deal with the second query. The choice is yours.
So i have two queries to give me the information i need and im trying to figure out the best way to get the result from them. i have a table of Holdings. using:
SELECT symbol, sum(shares) AS shares, sum(shares * price) AS cost
FROM Transactions
WHERE (action <>5)
AND date <= '2010-10-30'
GROUP BY symbol HAVING sum(shares) > 0
which results in
AGNC 50.00 1390.0000
RSO 1517.00 9981.8600
T 265.00 6668.7500
I then have another query
SELECT close FROM History WHERE symbol = $symbol AND date = $date
which will return the closing price of that day.
T 24.40
i want to basically for a given date calculate value so sum(shares * close) for each symbol. but i dont know how to do that with out looping through each row in php. i was hoping there was a join or something i could do in sql to make the data return the way i want it
I think you could do something similar to this: A select query selecting a select statement Put your second query literally in your first query. Not sure about exact syntax, but like:
SELECT
symbol,
sum(shares) AS shares,
sum(shares * price) AS cost,
sum(shares * close) as value
FROM Transactions
INNER JOIN History ON (symbol = $symbol AND date = $date)
WHERE (action <>5)
AND date <= '2010-10-30'
GROUP BY symbol HAVING sum(shares) > 0
#popnoodles is right about your naming though. If you use date I'd think you'd need [date].