How to multiple select in same table, in single query - mysql

I want a single query to multiple select, my purpose is to reduce mysql process, so I use this query, but it does not work:
SELECT *
FROM `my_setting`
WHERE `setting` = `site_url`
AND `setting` = `site_name`
I use select UNION query, but I think it doesn't reduce memory.

I think you want either:
SELECT * FROM my_setting
WHERE `setting` in ('site_url', 'site_name') -- if site_url is a literal string
or
SELECT * FROM my_setting
WHERE `setting` in (site_url, site_name) -- if site_url is a column name

Try this:
SELECT * FORM `my_setting` WHERE (`setting` = `site_url` OR `setting` = `site_name`)
Should get you where you need to go.

Related

How do I reuse query results in MSSQL?

What I would like to do is run a query, for example:
SELECT RouterCallKey
FROM Termination_Call_Detail
WHERE DateTime > CONVERT(DATE,GETDATE())
I then would like to use the information in the RouterCallKey column for the following query:
SELECT EnterpriseName
FROM Termination_Call_detail
INNER JOIN Agent
ON Termination_Call_Detail.AgentSkillTargetID = Agent.SkillTargetID
WHERE RouterCallKey = $Data from 1st query.
I would prefer not to use a stored procedure, I want to be able to transport the query easily.
A simple way to use results from one query in another is a subquery:
WHERE RouterCallKey IN
(
SELECT RouterCallKey
FROM Termination_Call_Detail
WHERE DateTime > CONVERT(DATE,GETDATE())
)

How do I optimize these SQL queries?

I have 1 variable which is used as WHERE condition in 3 initial queries. Based on the response from one of those I need to create queries to 2 more queries.
$var1 = $_GET['id']; $var2 = $_GET['truck'];
//first two are easy
SELECT `name`, `address` FROM `company` WHERE `id`='".$var1."' LIMIT 1; //q1
SELECT `value`, `date` FROM `checks` WHERE `truck`='".$var2."'; //q2
//the 3rd query may have multiple results and for every result i need **q4** and **q5** to be executed
SELECT `loadNumber`, `cfnNumber` FROM `loads` WHERE `truck`='".$var2."' ; //q3
//notice that WHERE conditions use values from **q3**
SELECT `value`, `date` FROM `finances` WHERE `load`='".loadNumber."'; //q4
SELECT `address` FROM `stops` WHERE `load`='".loadNumber."'; //q5
My question is about optimization as I am trying to combine all these queries into one if possible, hoping it will mean lesser server load time. I am not very familiar with JOINS, so ultimately this is how my code looks like with real data. And here is the result, also with real data. Is there a need to optimize/join these queries in order to decrease server load?
I would suggest to combine just last 3 queries, keep first 2 as is:
SELECT
l.`loadNumber`, l.`cfnNumber` ,
f.`value`, f.`date`,
s.`address`
FROM `loads` l
LEFT JOIN `finances` f
ON f.`load`= l.loadNumber
LEFT JOIN `stops` s
ON s.`load`= l.loadNumber
WHERE `truck`='".$var2."'
Do a subquery. Be something like this:
SELECT `item1_t1`, `item2_t1` FROM `table1` WHERE `item2_t1` in (SELECT `item1_t2` FROM `table2` WHERE `item2_t2`='".$var."');

Make query inner query result selectable

I was wondering if it is possible using MySQL to somehow, wrap the select of the inner query so that the outer query can use it in its where clause.
SELECT
`FirstName`,
`Surname`,
`AddressLine1`,
`AddressLine2`,
`AddressLine3`,
`AddressLocale`,
`AddressRegion`,
`AddressZip`,
`AddressCountry`,
`CopyShipAddress`
FROM `Contacts`
WHERE `EEID`
IN
(SELECT CONCAT_WS(',', `Sender`, `Receiver`, `Buyer` ) AS EEID_list
FROM `Transactions`
WHERE `TransactionID` = 3)
Sender, Receiver and Buyer are EEIDs. Perhaps there is a function other than CONCAT_WS I can use that will provide me with this functionality.
dont use concat_ws records on IN query , it may not give correct data
concat_ws may work perfectly for IN query with integers but may not work for strings because they need to be enclosed in quotes '
try below instead
SELECT
`FirstName`,
`Surname`,
`AddressLine1`,
`AddressLine2`,
`AddressLine3`,
`AddressLocale`,
`AddressRegion`,
`AddressZip`,
`AddressCountry`,
`CopyShipAddress`
FROM `Contacts`
WHERE `EEID`
IN
(
select Sender as eeid FROM Transactions WHERE TransactionId=3
UNION ALL
select Receiver as eeid FROM Transactions WHERE TransactionId=3
UNION ALL
select Buyer as eeid FROM Transactions WHERE TransactionId=3
)

How to optimize double select query

SELECT `id`, `field2`, `field3`, `field4` as Title FROM `articles`
WHERE `category_id` = 'X'
AND `id` NOT IN
(SELECT `articleid` FROM `article-seen` WHERE `userid` = 'Y')
How can I optimize this?
I think double select is bad, but im new to mysql
Try using JOIN that will get you the same result but makes the query looks simpler
The optimization depends (I think) on the version of MySQL you are using.
This is how you write it as a join:
SELECT `id`, `field2`, `field3`, `field4` as Title
FROM `articles` a left outer join
`articles_seen` arts
on a.id = arts.articleid and arts.userid = 'Y'
where a.`category_id` = 'X' and
arts.id is null;
This query, at least, doesn't materialize the subquery, which (I think) your originally query would.
To makes this faster, you want to add indexes. The ones that come to mind are: articles(category_id, id) and articles_seen(userid, articleid). You could also add the fields in the select to the first index, so the entire query can be satisfied by looking at the index, rather than returning to the main table.

Strange Mysql Query Performance on View

I have a view : vcompanyendofday
The following query executes in just 0.7 secs
Select * from vcompanyendofday
But a simple where condition to this query takes around 200.0 secs
select * from vcompanyendofday where companyid <= 51;
This is the view definition:
CREATE VIEW `vcompanyendofday` AS
select `c`.`companyid` AS `companyid`,
`c`.`scripcode` AS `scripcode`,
`e`.`eoddate` AS `eoddate`,
`e`.`prevclose` AS `prevclose`,
`e`.`delqty` AS `delqty`
from (
`company` `c`
left join
`endofday` `e`
on ((`c`.`companyid` = `e`.`companyid`)))
where (`e`.`eoddate` =
(
select max(`e2`.`eoddate`) AS `max(eoddate)`
from `endofday` `e2`
where (`e2`.`companyid` = `c`.`companyid`)
)
);
Seems you don't have an index on endofday.companyid
When you add the condition, company becomes leading in the join, and kills all performance.
Create an index on endofday.companyid:
CREATE INDEX ix_endofday_companyid ON endofday(companyid)
By the way, if you want all companies to be returned, you need to put the subquery into the ON clause of the OUTER JOIN, or your missing endofday's will be filtered out:
CREATE VIEW `vcompanyendofday` AS
select `c`.`companyid` AS `companyid`,
`c`.`scripcode` AS `scripcode`,
`e`.`eoddate` AS `eoddate`,
`e`.`prevclose` AS `prevclose`,
`e`.`delqty` AS `delqty`
from (
`company` `c`
left join
`endofday` `e`
on `c`.`companyid` = `e`.`companyid`
AND `e`.`eoddate` =
(
select max(`e2`.`eoddate`) AS `max(eoddate)`
from `endofday` `e2`
where (`e2`.`companyid` = `c`.`companyid`)
)
Have you tried the select used to create the view by itself with the WHERE clause to see what happens?
If the problem happens with that, run EXPLAIN on that query to see what's happening.
At a guess, there's no index on companyid in one of the tables, most likely endofday.