mySQL join Feature - php

I am using a PHP mySQL query. Struggling with a Joins.
mysql_query("SELECT * FROM electors WHERE telephone > 0")
Then I need it to select from a table called voting_intention with matching ID from the electors table of this query and get a column called pledge that is equal to 'C' OR 'D'.
How do we do this in 1 mySQL query.

SELECT * FROM electors e
JOIN voting_intention vi ON (e.id = vi.id)
WHERE e.telephone > 0 and
vi.pledge in {'C','D'};

Your possible SQL statement will be,
SELECT voting_intention.pledge
FROM electors, voting_intention
WHERE
electors.id =voting_intention.id
and electors.telephone > 0
and voting_intention.pledge in {'C','D'}

Related

Operator value in where condition php mysql

How can I do this?
SELECT sum(buy-sale) as istock form tbl_product
where istock > 0
I am not interested in the following method.
SELECT sum(buy-sale) as istock form tbl_product
where sum(buy-sale)> 0
You just have to replace WHERE by HAVING ;-)
SELECT SUM(`buy-sale`) AS `istock` FROM tbl_product HAVING `istock` > 0
But this will return the sum of all your records. Normally you do that when you group by a criteria, such as a product id or whatever. For example:
-- Find big orders
SELECT
`order_number`,
SUM(`quantity_ordered`) AS `quantity_sum`,
SUM(`product_price` * `quantity_ordered`) AS `total`
FROM
order_details
GROUP BY
`order_number`
HAVING
`total` > 1000;
This might help: https://www.mysqltutorial.org/mysql-having.aspx

Solution for "Subquery returns more than 1 row"

I'm trying to substitute my join SQL code to a different code without any of JOIN statements for faster data retrieval. However, i'm getting the error below.
#1242 - Subquery returns more than 1 row
What i would like to do, get all rows from one table 'tbl_my_itemlist' and JOIN to other more tables, tbl_register and tbl_register without using JOIN statements.
The Code using JOIN statement (works fine).
SELECT
tbl_screenshots.screenshot_image_url,
mit.my_itemlist_id,
mit.item_name,
mit.item_initial_cost,
mit.item_offer_cost,
mit.offer_date_from,
mit.offer_date_to
FROM
(
SELECT
my_itemlist_id
FROM
tbl_my_itemlist
WHERE
offer_date_from >='2020-10-20' AND offer_date_to <= '2020-10-30' AND
item_deleted_status = 'active'
) mlist
JOIN tbl_my_itemlist mit ON
mit.my_itemlist_id = mlist.my_itemlist_id
RIGHT JOIN tbl_screenshots ON mit.my_itemlist_id =
tbl_screenshots.my_itemlist_id
RIGHT JOIN tbl_register ON tbl_register.register_id = mit.register_id
GROUP BY
mit.my_itemlist_id
ORDER BY mit.offer_date_to ASC LIMIT 2
The code i'm substituting the JOIN statement code with.
SELECT
mit.my_itemlist_id,
mit.item_name,
mit.item_initial_cost,
mit.item_offer_cost,
mit.offer_date_from,
mit.offer_date_to,
(
SELECT
reg.business_name
FROM
tbl_register reg
WHERE
reg.register_id = mit.register_id
) reg_sql,
(
SELECT
sshots.screenshot_image_url
FROM
tbl_screenshots sshots
WHERE
sshots.my_itemlist_id = mit.my_itemlist_id
) sshots_sq
FROM
tbl_my_itemlist mit
WHERE
mit.offer_date_from >= '2020-10-20' AND mit.offer_date_to <= '2020-10-30' AND mit.item_deleted_status = 'active'
GROUP BY
mit.my_itemlist_id
ORDER BY
mit.offer_date_to ASC
LIMIT 2
I'm trying to build an SQL query that can retrieve data from million records within very short period of time as compared to using the JOIN statement.

Count the number of rows and back result if is greater than 0

I have try to display result only if content > 0
SELECT actors.*, (SELECT count(*) FROM `content_actors`
WHERE content_actors.actor = actors.record_id) AS getCount
FROM actors
If i try to add in query
WHERE getCount > 0
I will have error
Unknown column 'getCount' in 'where clause'
In MySQL, you can use a having clause:
SELECT actors.*,
(SELECT count(*) FROM `content_actors` WHERE content_actors.actor = actors.record_id) AS getCount
FROM actors
HAVING getCount > 0;
This is a MySQL extension.
Assuming actors.record_id is unique (presumably a primary key), this could be written as:
SELECT a.*, COUNT(*) as getCount
FROM actors a JOIN
content_actors ca
ON ca.actor = a.record_id
GROUP BY a.record_id; -- assumes this is unique/primary key
No filtering is needed, because the JOIN requires at least one match.

Mysql fieds list error

I have one mysql query but when i'm trying to work showing me that error
Unknown column 'ps_address.phone_mobile' in 'field list'
Mysql Query is
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid,
ps_orders.date_add,
ps_address.phone_mobile
FROM
ps_orders JOIN ps_customer on ps_orders.id_customer = ps_customer.id_customer
WHERE ps_address.id_customer=ps_orders.id_customer and
ps_orders.total_paid > 1
AND ps_orders.id_customer IN (
SELECT
ps_orders.id_customer
FROM
ps_orders
GROUP BY
ps_orders.id_customer
HAVING
COUNT(1) < 2
)
You need to specifiy the table you're selecting FROM. So if the column exists, here's the updated query (abbreviated):
SELECT
ps_orders.id_customer,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_orders.total_paid,
ps_orders.date_add,
ps_address.phone_mobile
FROM
ps_address, ps_orders
JOIN
ps_customer on ps_orders.id_customer = ps_customer.id_customer
WHERE
ps_address.id_customer=ps_orders.id_customer AND
ps_orders.total_paid > 1 AND
ps_orders.id_customer IN (...)
This syntax error means, you do not have this column "phone_mobile" in this table "ps_address".
The column does simply not exist?
Your script has syntax error. Column name "1" does not really exists in
COUNT(1) < 2
must be: COUNT(column_name) < 2
or COUNT(*) <2
COUNT(ps_orders.id_customer) < 2

Returning Just count of records found in MYSQL query

I have the following MYSQL query which returns the number of photos found for each record where the number of photos is greater than 0.
SELECT advert_id, (SELECT COUNT( * ) FROM advert_images b WHERE b.advert_id = adverts.advert_id) AS num_photos
FROM adverts
WHERE adverts.approve = '1'
HAVING num_photos > 0
The query works fine, but I want to just return a count of the records found. i.e. the number of records which have at least one photo. I've tried to wrap the whole query in a COUNT, but it gives an error. I want to do this in the query, and not a separate count of records found in php.
SELECT COUNT(*) AS TotalRecords
FROM
(
SELECT a.advert_id, COUNT(*) AS num_photos
FROM adverts AS a
JOIN advert_images AS i
ON i.advert_id = a.advert_id
WHERE a.approve = '1'
GROUP BY a.advert_id
HAVING num_photos > 0
) AS mq
SELECT COUNT(*) FROM (SELECT advert_id, (SELECT COUNT( * ) FROM advert_images b WHERE b.advert_id = adverts.advert_id) AS num_photos
FROM adverts
WHERE adverts.approve = '1'
HAVING num_photos > 0) AS c
This should do the trick

Categories