mysql: Table alias doesn't exist - mysql

So I have a table in my database (which I'll call Test). The table is named Items, and has an int column named itemID and a decimal(8, 2) column named currently. They represent a unique ID for an item and the current bid price on an item, respectively. The currently column is able to be NULL because there may not have been a bid on an item yet.
My goal is to get the itemID of the item with the highest current bid that has at least one bid. I have a mysql query that looks as follows:
select itemID
from (select itemID from Items where numBids > 0) as b
where currently = (select max(currently) from b);
When I execute this in mysql, I get an error that says:
ERROR 1146 (42S02): Table 'Test.b' doesn't exist
Can anyone explain what's going on here? I haven't used mysql in a while so I'm rusty. Do I have to do a join of Items with itself in order to accomplish something like this? Any help would be appreciated.

The alias "b" is not being seen by the last subquery. Insted to use that complicated query use
SELECT itemID
FROM Items
WHERE numBids > 0
ORDER BY currently DESC
LIMIT 1
To handle the situation where multiple items have the highest current bid:
SELECT itemID
FROM Items
WHERE numBids > 0
AND currently =
( SELECT MAX(currently)
FROM Items
WHERE numBids > 0
)

The problem is that you cannot use the b inside the select of the where clausole.

Related

SELECT data from multiple tables if a requirement is met in second table

the title doesnt describe it that well, my problem:
I have 2 tables, one table for orders, the other for the product.
An order can have n products associated with it.
I want to select those orders, where all their associated products have a status (attribute of the product) greater or equal to x. (So I know that every product of my order is "ready" and the order can be processed further)
Every ordered product has an OrderID
Any tips?
e: Just started with SQL, dont bash me if this is a stupid question
It's a matter of mindset.
You have to find the 'dual' form of your question ( -> double negation).
You need to find all the orders that have AT LEAST one line that is not ready.
Assuming your tables are the common:
Order(ID,bla,bla,bla) and Order Line(orderID, row#, status, bla, bla) FK orderid references order.
You can use this stub:
Select *
from orders O
where not exists ( select * from order_line OL
where ol.orderID=O.orderID --binding with outer query
and status <> 'ready'
)
SIDE NOTE: my query will produce also empty orders, to filter them just add to outer query and exists (select * from orderline oe where oe.orderid=o.orderid)

Advanced Search Functionality - SQL

i'm working on an advanced search functionality on my website.
Basically data I'm working on is stored within two tables.
First table contains basic information about the product (1 row = 1 product so it's unique).
Table structure may look like this:
id, title, description
The second table contains more information about the product. The product may but don't have to have any rows here. However, one product may store in the second table a few rows. What's more - data in the second table should be used to the advanced search functionality. Table structure may looks like this:
id, item_id (from table 1), value_id (from another table), value
I want to select only these products (table 1) which has specified value_id (from column 2):
... WHERE table1.item_id = 5 AND table2.value_id = 1000
As I mentioned before - table 2 may but doesn't have to contains any rows connected by item_id with the table 1.
I've tried to use JOIN/LEFT JOIN function in SQL but in this case when the product has 3 rows in the table 2 - a query result returns 3 rows instead of 1 or 0 (if not found any results).
How can I handle that?
You want to select products. So select from the product table. You want to select only those for which exists a certain attribute. So create an approriate WHERE clause. As you want to look up data in another table, you could use EXISTS or IN.
select *
from items
where id in (select item_id from item_values where value_id = 1000);

MYSQL - Removing number from One Table in SQL if it exists in another Table

I need some assistance with deleting data within an SQL Table if it matches data from another table.
There are two Tables
Table 1: DNC
Table 2: Call_Logs
Table 1 has only one column called phone_number.
Table 2 has multiple columns, but the main one that is important is also named phone_number.
Basically, I want to remove any numbers that are in Table 2 from Table 1, if they exist. Now, I don't want to delete every number from Table 1 if they exist in Table 2. What numbers I collect from Table 2 are based on some criteria.
To pull the data from Table 2 that I need to delete from Table 1, I use the following:
select phone_number from call_logs where call_date < 'DATE' and Status = 'DNC'
This query will give me a list of all phone numbers that I would want to remove from Table 1 if it exists.
EXAMPLE:
https://drive.google.com/file/d/0B4NE4ZDXd6steW5odWhBMDJSY1U/view
I am not sure how I would go about running the query in SQL. Any types would be appreciated it.
Looking to your sample in img
You could use a left join on table 2 (where table2.phone_number is null alias don't match)
delete from table1
left join table2 on table1.phone_number = table2.phone_number
where table2.phone_number is null
correlated subquery w/ an exists so it can early exit
The select 1 in the subquery is because we have to select a value but it doesn't matter what value that is. since the coloration (DNC.Phone_Number = CL.Phone_Number) is all we are after; along with your limits on call_log.
DELETE
FROM DNC
WHERE exists (SELECT 1
FROM Call_logs CL
WHERE CL.call_date < 'DATE'
and CL.Status = 'DNC'
and DNC.Phone_Number = CL.Phone_Number)

Yii2 Active Record get the latest record

I have the following table.
As you can see there are duplicate school_id. The id field is auto increment so in order to get the latest, I need to get the row with the highest id per school_id.
Using raw sql, the query is like this,
SELECT
rps_checklist.id, school_id,application_type,school_year
FROM rps_checklist
INNER JOIN (
SELECT id, MAX(id) AS maxx FROM rps_checklist GROUP BY school_id
) ms ON rps_checklist.id = maxx
and the result I need is the following:
P.S I am passing this as searchModel

Strange behavior in SQL with CASE WHEN EXIST in subquery (MySQL 5.5)

I've got a shop with items and itemgroups.
I also got some additional items, from which one should randomly be selected to present it in the cart overview, if that one is not present in the cart allready.
There can be items linked to the items group as well. If there is no item linked to the items in the cart i want one of them, that is not allready inside the cart to be randomly selected
I save relations between items inside table item2item:
itemid INT
additional_item_id INT
I save relations between groups and items in table group2item
groupid INT
additional_item_id INT
To make it a little more simple let's assume my item table looks like this:
itemid INT
name VARCHAR(100)
Here is what i tried to get an additional item:
SELECT
name
FROM
items a
WHERE
(a.itemid) = (
# if we have any additional items linked to the item get one of em randomly, that is not inside of a cart
SELECT
CASE WHEN EXISTS (
SELECT
b.additional_item_id
FROM
item2item b
WHERE
b.additional_item_id NOT IN (10)
AND b.itemid IN (10)
)
THEN (
SELECT
c.additional_item_id
FROM
item2item c
WHERE
c.additional_item_id NOT IN (10)
AND b.itemid IN (10)
ORDER BY
RAND()
LIMIT 1
# else if we have additional items linked to the items group get one of em randomly, that is not inside of a cart
) ELSE (
(
SELECT
d.additional_item_id
FROM
group2item d
WHERE
d.additional_item_id NOT IN (10)
AND
d.groupid IN (1)
ORDER BY RAND()
LIMIT 1
)
)
END as selecteditemid
)
Anyone can explain to me, why i get different amounts of rows with this?
You are asking why you might get different numbers of rows. Here are some thoughts:
items.itemid is not unique, so the duplicates are coming from multiple matches.
The else clause is executed and the where clause filters out all rows.
The else clause is executed and group2item.additional_item_id matches no items.itemid.
I speculate that it might be possible that when the first condition in the case statement is executed, the data might change between the when and then.
If you are looking for a fix to this, then move the subqueries to the from clause and put simpler logic in the where.