SELECT count mysql - mysql

I have a database called Real_Estate_Lab_Project, I have a table call listings, with a column named listing price.
listing_key int PK
listing_status int
listing_type int
date_listed date
date_expires date
date_unlisted date
reason_unlisted int
address varchar(50)
city varchar(30)
state char(2)
zip_code varchar(10)
lot_number varchar(50)
residential_area int
listing_price int
listing_agreement_signed_date date
remark varchar(1000)
I am trying to run
USE Real_Estate_Lab_Project;
SELECT count(*)
as no_of_listings,
avg(price) as avg_listing_price, sum(listings)
as sum_of_listings, min(price)
as min_price, max(price)
as max_price
from Listing
where status = 'Active'
When I run script it is telling me
Error Code: 1146. Table 'real_estate_lab_project.listing' doesn't exist 0.000 sec, but I know it exists as I can see it when I do
SELECT * from listings.
The date expires column does have some that are expired, but some are not.
What am I missing?

it looks you have to replace 'Listing' with listings in your sql
SELECT count(*)
as no_of_listings,
avg(price) as avg_listing_price, sum(listings)
as sum_of_listings, min(price)
as min_price, max(price)
as max_price
from listings
where status = 'Active
'

Related

mysql query to select all objectives of a goal and those objective which are done

goal id total occurance of goal id total occurance when status is 1
1 5 3
This is schema of the table
CREATE TABLE `goal_objectives` (
`objective_id` int(11) NOT NULL ,
`objective_name` varchar(255) NOT NULL,
`objective_description` tinytext NOT NULL,
`goal_id` int(11) NOT NULL,
`objective_status` tinyint(4) NOT NULL
);
select goal_id, count(objective_status)as objective_done
from goal_objectives
where objective_status='1' group by goal_id;
select goal_id,count(goal_id) as total_current_goals
from goal_objectives
group by goal_id
order by goal_id DESC ;
I just want to show the combine result of these two queries.
Individually it returns required result but when i try to merge them is does not work
See the output in the link below:
https://i.imgur.com/6Rnac89.png
Use conditional aggregation:
select goal_id, count(*) as total_current_goals,
sum( objective_status = 1 ) as objective_done
from goal_objectives
group by goal_id
order by goal_id desc ;
Note that objective_status is a number. The comparison value should be a number, not a string.

mysql returning more than one row

I have a mysql query that needs to return the supplier ID from the supplier table by searching with the supplier name but it keeps returning multiple values.
CREATE DEFINER=`root`#`%` PROCEDURE `sp_insert_sup_order`(
supname varchar(50),
dat date,
total decimal(10,2)
)
BEGIN
insert into Supplier_Order
(
Supplier_ID,
SupDate,
Total,
Sup_Name
)
values
(
(select Supplier_ID from Supplier Where Supplier_ID.SupName = supname ),
dat,
total,
supname
);
Thats the query. Any help with this will be appreciated thanks
(select Supplier_ID from Supplier Where Supplier_ID.SupName = supname )
this should be like this:
(select Supplier_ID from Supplier Where Supplier.SupName = supname ),
And why do you store the supplier name and the id in the order ? that should not be done, image you have to change the name of the supplier, you will have to update all orders. Only the supplier id should be stored in the order table!

Return the number of rows expected for a group by query

I would like to get the number of rows returned from a mysql query that uses group by.
table
CREATE TABLE IF NOT EXISTS TestRunSteps (
`idTestRunSteps` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`idUsersExecBy` VARCHAR(10) NULL ,
`LastExecUserIPV4` INT UNSIGNED NULL ,
PRIMARY KEY (`idTestRunSteps`);
SELECT count(*)
from proj1_db.TestRunSteps
group by idUsersExecBy,LastExecUserIPV4
returns
3,000002,3232236222
1,000003,3232236222
5,000004,3232236222
What I would like to have is a simple 3 - for 3 rows. Please tell me how
The number of groups is the number of distinct combinations of the columns grouped by. The query that returns that number is:
select count(distinct idUsersExecBy, LastExecUserIPV4)
from proj1_db.TestRunSteps
select count(*)
from
(SELECT count(*)
from proj1_db.TestRunSteps
group by idUsersExecBy,LastExecUserIPV4) as temp

MySQL : Return random value from each column

Note: This might be a strange question.
I have a table containing first name and last name, which schema as follow (table name: random_names):
id INT PRIMARY, AUTO INCREMENT
first_name VARCHAR(100) NOT NULL
last_name VARCHAR(100) NOT NULL
I would like to use a query to fetch a random value from first_name and last_name. Currently I use 2 queries to fetch the value:
SELECT first_name FROM random_names ORDER BY rand()
SELECT last_name FROM random_names ORDER BY rand()
But I wish I can output a list of random results in 1 result output. What did I miss ?
select
(select first_name from random_names order by rand() limit 1) as random_first_name,
(select last_name from random_names order by rand() limit 1) as random_last_name;
though for tables of any size it is much faster if you programmatically determine the number of entries and pick a random offset for each column:
select
(select first_name from random_names order by rand() limit $first_name_offset,1) as random_first_name,
(select last_name from random_names order by rand() limit $last_name_offset,1) as random_last_name;
where the offsets are a random number from 0 to one less than the result of select count(*) from random_names.
Followup question:
but how about list out result count equal to the number of values in original table? (just like shuffle the data in the table)
I'd do that like this:
create temporary table rand_last (id int(11) primary key auto_increment, last_name text) select last_name from random_names order by rand();
create temporary table rand_first (id int(11) primary key auto_increment, first_name text) select first_name from random_names order by rand();
select first_name, last_name from rand_first inner join rand_last using (id);
or possibly like this (assuming random_names has an 'id' primary key):
create temporary table rand_one (id int(11) primary key auto_increment, random_names_id int(11)) select id random_names_id from random_names order by rand();
create temporary table rand_two (id int(11) primary key auto_increment, random_names_id int(11)) select id random_names_id from random_names order by rand();
select rand_first.first_name, rand_last.last_name from rand_one inner join rand_two using (id) inner join random_names rand_first on rand_one.random_names_id=rand_first.id inner join random_names rand_last on rand_two.random_names_id=rand_last.id;
You can get all possible pairs of first_name and last_name in random order by following query:
select *
from (select first_name from random_names) a,
(select last_name from random_names) b
order by rand();
You can also do like this-
SELECT first_name, last_name FROM random_names ORDER BY rand()
OR
SELECT concat(first_name," ",last_name) fullname FROM random_names ORDER BY rand()

mysql query with subquery loop?

I have a query
SELECT
count(product) as amount,
product,
sum(price) AS price
FROM `products`
WHERE
brid = 'broker'
AND
cancelled is null
GROUP BY product
WITH ROLLUP
Is it possible to query a table to get a brokers id and then for each broker run the query above written as 1 query?
Almost like:
SELECT brid FROM membership
THEN
SELECT
count(product) as amount,
product,
sum(price) AS price
FROM `products`
WHERE
brid = membership.brid
AND
cancelled is null
GROUP BY product
WITH ROLLUP
THEN
SELECT NEXT brid
Is this possible? i know how to do it in PHP but i would prefer 1 query that can create an array rather than tons of queries for each.
Thanks
Adam.
Sure, you can GROUP BY both the 'brid' field and the 'product' field. As noted below, WITH ROLLUP will cause it to sort by 'brid' and then by 'product':
SELECT
brid,
count(product) as amount,
product,
sum(price) AS price
FROM `products`
WHERE
brid IN (SELECT brid FROM membership)
AND
cancelled is null
GROUP BY brid, product
WITH ROLLUP
SELECT
count(product) as amount,
product,
sum(price) AS price
FROM `products`
INNER JOIN membership on membership.brid = products.brid
WHERE
cancelled is null
GROUP BY product
WITH ROLLUP
As far as I can understand from your example, all you need is inner join between membership and products on brid
Take the following example:
products table:
CREATE TABLE `test` (
`price` int(11) DEFAULT NULL,
`product` varchar(20) DEFAULT NULL,
`brid` varchar(20) DEFAULT NULL,
`cancelled` varchar(20) DEFAULT NULL
)
membership table:
CREATE TABLE `membership` (
`brid` varchar(20) DEFAULT NULL
)
And following is my query as you required:
SELECT
t.brid, count(t.product) as amount,
t.product,
sum(t.price) AS price
FROM products t, membership m
WHERE
t.brid = m.`brid`
AND
cancelled is null
GROUP BY product
Hope that helps!