No tables used? - mysql

I've been working with this bit of code for some time now, and now, after some tinkering, I thought I finally got it to work, then it gives me this error:
Error Number: 1096
No tables used
SELECT *
and this is the code
SELECT start.*, posts.did, COUNT(posts.pid)
FROM akia_starting_posts AS start
JOIN posts
ON posts.did = start.did
JOIN akia_users AS users
ON users.username = start.username
I'm pretty sure at the * my start is being used, so what is it talking about? It couldn't be any other code in the file, since when I take out this bit of code, it starts working.

Try to add backticks on sql string that has the word start on it. To begin try to use the one table in question first like.
SELECT 'start.*' FROM akia_starting_posts as 'start'
and if it works then you may incorporate it in fullscale join like the one you posted
with bacticks used.

Related

MySQL return no results from select * where varchar="" query

I'm having issue with my study project for creating database in MySQL.
I've imported data using LOAD to my created table from a CSV file. Now when I'm executing select * from mytable everything show up perfectly, but when I execute select * from bi_jogging.routes as r where r.Creator_Email="jhenderson7c#bbb.org"
I get nothing.
The email is there, I've tried different syntax options but it seems to be something else, I suspect something with the varchar format, I really have nothing in mind.
For other tables it works fine and others not.
You can try using the query:
select * from bi_jogging.routes as r where r.Creator_Email like "%jhenderson7c#bbb.org%"
If like operator shows the result then there may be white spaces in the email, please double check..
For join try this:
select * from bi_jogging.routes as r join bi_jogging.buddies as b
on b.Email like '%r.Creator_Email%'
I think it should work. Again check with same code.
select * from bi_jogging.routes as r where r.Creator_Email='jhenderson7c#bbb.org'
if [select * from mytable] this works ,then try to copy the email from result and paste it in where clause.
There may be conflicts between quotes.
your table entry contains quotes???
check properly. i think you have quotes in your table entry,so when you try this,
select * from bi_jogging.routes as r where r.Creator_Email like "%jhenderson7c#bbb.org%"
'%' sign matches with any character and you will get the result.
Inside the tablejhenderson7c#bbb.org and "jhenderson7c#bbb.org" are completely different.
I found spaces in the mysql tables after few emails, so I guess that was it. burned 8 hours on this one, thank you all. I could not find the spaces at the end of the mail by looking at it, I had to hit backspace to see that only after two hits the last char is deleted
this helped me : UPDATE bi_jogging.results set Mail_Found = TRIM(Replace(Replace(Replace(Mail_Found,'\t',''),'\n',''),'\r',''));

2 identical mysql select queries in if statement, first works second does not

I have the following 2 mysql select queries within a PHP if statement:
if ($chooselocn =="") {
$query = "
SELECT $table.*, outcodepostcodes.lat, outcodepostcodes.lng
FROM $table
LEFT JOIN outcodepostcodes
ON UPPER($table.postcode)=outcodepostcodes.outcode
WHERE
$where_no_and
AND
(hide='0' OR hide IS NULL OR hide='')
ORDER BY rent $reihenach LIMIT $offset, $rowsPerPage
";
}
else {
$query = "
SELECT $table.*, outcodepostcodes.lat, outcodepostcodes.lng
FROM $table
LEFT JOIN outcodepostcodes
ON UPPER($table.postcode)=outcodepostcodes.outcode
WHERE
$where_no_and
AND
outcodepostcodes.lat <= $latpoint
AND
(hide='0' OR hide IS NULL OR hide='')
ORDER BY rent $reihenach LIMIT $offset, $rowsPerPage
";
}
The first query works but the second returns this error message:
Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND outcodepostcodes.lat <= 51.491239000000000 AND (hide='0' OR hide IS NULL OR' at line 8
Even if I remove the:
AND
outcodepostcodes.lat <= $latpoint
from the 2nd query making the two identical I still get similar error msg in the second.
Any ideas greatly appreciated.
You probably have an empty $where_no_and variable, thus your second query gets to contain ... WHERE AND ... which is not valid SQL
I bet the variable $where_no_and causes the issue.
It seems like it is empty causing the WHERE statement to be followed by the AND.
So this line:
WHERE $where_no_and AND outcodepostcodes.lat <= $latpoint AND (hide='0'...
is like this:
WHERE AND outcodepostcodes.lat <= $latpoint AND (hide='0'...
The WHERE AND is not valid syntax.
Even after you removed the part:
AND outcodepostcodes.lat <= $latpoint
Your line would look like this:
WHERE $where_no_and AND (hide='0'...
Which would result in this:
WHERE AND (hide='0'...
Again you have WHERE AND which is not valid syntax.
Try to make sure that the $where_no_and variable is not null or empty.
If your business logic says that this variable COULD be empty then you have to write a couple of more lines of code to handle this case.
Cheers!
This is not a solution but a way to find the bug.
Just put a var_dump($query) after the if(). It's impossible to tell without knowing what the variables actually contain. The dump will be before the query is executed, so you'll see very well what is going on there.
If the query is still apparently correct post the dump here and we'll see.
Why do if/then if the queries are supposed to be identical regardless of the condition?
That said, your variables that form the query are probably different depending on the value of $chooselocn. I would start by dumping the actual query into debugging log or the browser as well as the variables that compose it and it would shed some light.
I should also add the standard warning about watching out for SQL injections.
Thank you, thank you, thank you and a MASSIVE THANK YOU to every one who replied to my question!!!!
#Gordon Linoff, thank you for being the first to respond in the right direction
#David Lavieri, thank you for the useful suggestion
#Tudor Constantin, thank you for being the first to explain the crux of the problem
#pid, thank you for the useful suggestion
#user2399432, a massive thank you for the very lengthy, detailed and exhaustive explanation of EXACTLY what was going on.
While everyone pointed me in the right direction, and unfortunately I was busy elsewhere to follow up on those early suggestions, I must upvote #user2399432 for going to all that trouble to make absolutely sure that it all sunk in and there were no two ways about it.
Just as background info, this is an extremely old site I have come back to work on, after a three year absence from coding, and I noticed that I had the following lines of code in this particular script:
///THIS IS THE CRUCIAL LINE BELOW:
$where_no_and = rtrim($where, 'AND ');
///End of crucial line and then TEST
//var_dump($where_no_and);//VERY USEFUL DIAGNOSTIC! INDISPENSIBLE! MUST RETAIN! DO NOT DELETE!
//echo "#6 City is:".$lc_city;//USEFUL DIAGNOSTIC
So I must have had the same problem those many years ago and dealt with it accordingly using var_dump($where_no_and);
Be that as it may, I was well and truly stuck this time round and your valuable help has knocked down the barricades, SO THANK YOU TO ALL!

MySQL Left Outer Join and {?}

I have been tasked with converting some Crystal SQL queries into QlikView and am having trouble deciphering the SQL code as it has been a decade since I last played with this. I'm modelling the data in MySQL workbench prior to importing it into QlikView.
I have posted the code bellow for the sake of completeness although I realize a lot of it is surplus.
The issue I am having is I don't know and cant seem to work out how do duplicate these queries in MySQL workbench as I don't understand what these code segments are doing:
={?APS: ITEM1.ST_Prodcode} and ={?FKE: ITEM1_1.ST_Prodcode} etc.
The above mentioned code appears to me to be calling the next query. Am I right in thinking this?
Pricing
SELECT
`stock_management1`.`st_prodcode`,
`stock_management1`.`st_sdesc`,
`stock_management1`.`st_mstockist`,
`stock_management1`.`APS_rol`,
`stock_management1`.`APS_eoq`,
`stock_management1`.`APS_ms`
FROM
`pricing`.`stock_management`
`stock_management1`
WHERE
(`stock_management1`.`st_mstockist`='BRA'
OR
`stock_management1`.`st_mstockist`='FCS'
OR
`stock_management1`.`st_mstockist`='FKE')
AND
`stock_management1`.`APS_ms`>0
AND
(`stock_management1`.`st_prodcode`>='A'
AND
`stock_management1`.`st_prodcode`<='WZZZZZ999')
EXTERNAL JOIN
stock_management1.st_prodcode={?APS: ITEM1.ST_Prodcode}
AND
stock_management1.st_prodcode={?FCS: stocktake1.S_ProdCode}
AND
stock_management1.st_prodcode={?CENTRAL: Command.mv_PRODCODE}
APS
SELECT `ITEM1`.`ST_SOH`, `ITEM1`.`ST_Prodcode`
FROM `aps`.`ITEM` `ITEM1`
WHERE `ITEM1`.`ST_Prodcode`={?pricing: stock_management1.st_prodcode}
EXTERNAL JOIN ITEM1.ST_Prodcode={?FKE: ITEM1_1.ST_Prodcode}
FKE
SELECT `ITEM1_1`.`ST_SOH`, `ITEM1_1`.`ST_Prodcode`
FROM `iewkelvin`.`ITEM` `ITEM1_1`
WHERE `ITEM1_1`.`ST_Prodcode`={?APS: ITEM1.ST_Prodcode}
EXTERNAL JOIN ITEM1_1.ST_Prodcode={?FCS: ITEM1_2.ST_Prodcode}
Thanks for comments imran & ralfbecher.
I found that the {? implies WHERE followed by the Database: table.field
Simple as that really.

SELECT * FROM games WHERE

I am having issues with my MySQL syntax. I would like to run a select query where either one of two options are true. However the following code does not work.
SELECT * FROM games WHERE genre="indie" OR title="indie"
I have been fooling around and look at other threads and have found out how to use OR to check the same column for multiple entries but not a way to check different columns for the same entries. When I do:
SELECT * FROM games WHERE genre="indie"
The query works fine. Any help would be greatly appreciated.
The only way I see this really would't work, is if you've mistyped the name of the column 'title' (if the second query you wrote works)
The assumptions about the case sensitivity are wrong, since the second query returns something, the first should return at least the same rows as the second one
In MySQL " " works just as ' ', so this assuption was wrong too.
If you post more information, it would be easier to help you
Maybe you ignoring the upper/lower case? Also use like
You can use this:
SELECT * FROM games WHERE (LOWER(genre) like 'indie') OR (LOWER(title) like 'indie')

mysql group_concat in where

I am having a problem with the following query(if this is a duplicate question then i'm terribly sorry, but i can't seem to find anything yet that can help me):
SELECT d.*, GROUP_CONCAT(g.name ORDER BY g.name SEPARATOR ", ") AS members
FROM table_d AS d LEFT OUTER JOIN table_g AS g ON (d.eventid = g.id)
WHERE members LIKE '%p%';
MySQL apparently can't handle a comparison of GROUP_CONCAT columns in a WHERE clause.
So my question is very simple. Is there a workaround for this, like using sub-query's or something similar? I really need this piece of code to work and there is not really any alternative to use other than handling this in the query itself.
EDIT 1:
I won't show the actual code as this might be confidential, I'll have to check with my peers. Anyway, I just wrote this code to give you an impression of how the statement looks like although I agree with you that it doesn't make a lot of sense. I'm going to check the answers below in a minute, i'll get back to you then. Again thnx for all the help already!
EDIT 2:
Tried using HAVING, but that only works when i'm not using GROUP BY. When I try it, it gives me a syntax error, but when I remove the GROUP BY the query works perfectly. The thing is, i need the GROUP BY otherwise the query would be meaningless to me.
EDIT 3:
Ok, so I made a stupid mistake and put HAVING before GROUP BY, which obviously doesn't work. Thanks for all the help, it works now!
Use HAVING instead of WHERE.
... HAVING members LIKE '%peter%'
WHERE applies the filter before the GROUP_CONCAT is evaluated; HAVING applies it later.
Edit: I find your query a bit confusing. It looks like it's going to get only one row with all of your names in a single string -- unless there's nobody in your database named Peter, it which case the query will return nothing.
Perhaps HAVING isn't really what you need here...
Try
SELECT ...
...
WHERE g.name = 'peter'
instead. Since you're just doing a simple name lookup, there's no need to search the derived field - just match on the underlying original field.
GROUP_CONCAT is an aggregate function. You have to GROUP BY something. If you just want all the rows that have %peter% in them try
SELECT d.*, g.name
FROM table_d AS d
LEFT OUTER JOIN table_g AS g
ON (d.eventid = g.id)
WHERE g.name LIKE '%peter%';