I'm trying to put together a MYSQL query designed for an AJAX\PHP CMS, which goes a little like this:
SELECT table.info
FROM table
WHERE table.variable LIKE '%refinedby%' IN
(SELECT other valid subquery to select data from)
However, I keep tripping syntax errors near LIKE '%refinedby'IN (
If I use = rather than LIKE there's no problem, as the following:
SELECT table.info
FROM table
WHERE table.variable = 'refinedby' IN
(SELECT other valid subquery to select data from)
Does anyone have any ideas where I can't preceed a subquery with a LIKE selector?
Thanks in advance!
You can't use in like that you have to specify what is going to BE in
SELECT table.info
FROM table
WHERE table.variable LIKE '%refinedby%'
and table.variable IN (SELECT other valid subquery to select data from)
Related
I have a table with many columns let say column1,....,column20. I don't want to scroll everytime to the end of the result table to see the value of column20. In mssql I usually do
SELECT column20, * FROM TABLE but apparently this is not valid in MySQL. Any hints? (I also don't want to select all columns explicitly in the select statement)
You have to give the table name in your query, otherwise mysql complains :
SELECT column20, mytable.* FROM mytable
PS: I have absolutely no idea as to why, because SELECT *, column20 FROM mytable works just fine... Strange things happens sometimes ^^
I just wanted to simply count using where conditional and yet this query ask me for parameter instead of automatically execute the query
SELECT COUNT(ActDiscDischargingPort) from(
SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable) WHERE SchLoadVessel LIKE "XB24 - MV. MEMPHIS" AND SchLoadVoyageNo LIKE "0019";
What is the proper way of writing this query?
Found the answer, turn out the query has to be like this
SELECT COUNT(ActDiscDischargingPort) from ( SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable WHERE SchLoadVoyageNo LIKE "XB24 - MV. MEMPHIS" AND SchLoadVessel LIKE "0019" )
Every derived table must have its own alias.
The correct syntax would be
SELECT
COUNT(ActDiscDischargingPort)
from(
SELECT
DISTINCT ActDiscDischargingPort
FROM
SelisihLoadVSActualLoadTable
) AS T
WHERE
SchLoadVessel LIKE "XB24 - MV. MEMPHIS"
AND SchLoadVoyageNo LIKE "0019";
You can further speed up your query by optimizing it a bit.
My first select statement would be like below,
SELECT m.col_name
, m.col_alias
FROM <table_name> m
WHERE m.exportable LIKE '%Y%'
And I'm trying to create a second select query with the data that I'm receiving from the first statement, like below
SELECT tabella.id alias_1
, tabella.value alias_2
, **“list of col_name result of the previous query”
FROM <another_table> tabella
WHERE tabella.metadata_id = 'CI_INDEX||CI_01'*
Thanks in advance.
Try the Subquery.
You will find an example in the links.
If the tables have relation between them then go for Join
if the tables do not have relation you can use the following format.
select table1.col1,table2.col1 from table1, table2
where table1.colx like '%exp%' and table2.coly like '%exp2%'
So I have joined 3 queries together using UNIONs and want to count the number of lines in the result, but it's a bit weird. It actually works, and gives the correct answer, but it doesn't assign the "AS" part correctly.
SELECT COUNT(*) FROM (
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
)
AS NoOfTweets";
The outcome is correct, but instead of assigning it to "NoOfTweets" it assigns it to "Count(*)". If I remove the "AS NoOfTweets" it stops working. If I remove some brackets it stops working. I'm running low on ideas after a long day! I can post the whole code if needs be but would rather not as it's quite long and I think that bit works.
Thanks in advance, Jack.
Edit: Fixed with:
SELECT COUNT(*) NoOfTweets FROM (
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
)
AS NoOfTweets";
Thanks guys :)
You aren't putting it in the correct location. The beginning of your query should look like this:
SELECT COUNT(*) AS NoOfTweets
More on Column Alias
SELECT COUNT(*) NoOfTweets FROM
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
or
SELECT COUNT(*) AS NoOfTweets FROM
(Long Select Statement)
UNION
(AnotherLong Select Statement)
UNION
(Even Longer Select Statement)
You have to use AS exactly after the item you are counting:
SELECT COUNT(*) AS `NoOfTweets`
FROM ( ... )
Also be careful with the " you have near the end. Or maybe it comes from a longer string.
The error is Every derived table must have its own alias which is something I didn't know, so thanks for the education :)
http://sqlfiddle.com/#!9/d30f4/4
Nice of MySQL to give an explanation - I tried with MS SQL on SQLFiddle and just got Incorrect syntax near ')'. which isn't so helpful!
So, your 'NoOfTweets' is the name given to the results column, and also to the 'derived table' which is required by the SQL engine but could be a different name ... it's not returned in the results. The point of naming a derived table is in case you wish to JOIN to other tables and reference the fields in the joins.
I have a mysql query, something like this:
SELECT users*100 as totalusers, totalusers*90 AS totalsalerys FROM table
As you can see I want to reuse the totalusers when calculating totalsalerys so I son't have to write the same code again. That dosen't seem to work in mysql, is there another easy way to do this?
My query is just an example, change the *100 and *90 to some very long formula and you might see what i mean..
SELECT (#r := users * 100) as totalusers,
#r * 90 AS totalsalerys
FROM table
You can also use a subquery as #Tom Ritter advices, but it's not friendly to ORDER BY ... LIMIT ... clause.
In MySQL, all results of the inner subquery will be fetched before applying any filters in the outer subquery.
I believe you would have to copy/paste the formula or use a subquery. The below would work in T-SQL, but I imagine it'd work in MySQL as well since it's pretty simple.
SELECT
x.totalusers, x.totalusers*90 AS totalsalerys
FROM (
SELECT users*100 as totalusers FROM table
) x