First off, I'm a beginner at this so please be patient with me. I've tried searching the forums here, and elsewhere and from what I can see I'm doing it right.. but I get an error. to "Check syntax to use near 'LIKE CONCAT(games.tag, ' Special%') AT LINE X". So clearly I'm doing it wrong.. But everything I have found says I've done it right. Someone please help I'm getting frustrated.
SELECT
games.tag,
ribbons.name,
members.username,
members.joined
FROM
member_ribbons
INNER JOIN ribbons
ON member_ribbons.ribbon_id = ribbons.id
INNER JOIN games
ON ribbons.game_id = games.id
INNER JOIN members
ON member_ribbons.member_id = members.id
WHERE games.id = members.primary_game AND discharged='000-00-00' AND ribbons.name = LIKE CONCAT(games.tag, ' Special %');
An Example
ribbons.name will contain "BF4 Special Consideration"
I need to be able to find that specific one.. but the only thing that changes is the first word which is contained in games.tag. The rest of the Query works. Just need to figure out how to get it to do this and I'm good. However Searching google everywhere and here has not shown any examples of how to do this.. Is it even possible? Clearly I'm doing this wrong.
you have extra =
change this
AND ribbons.name = LIKE CONCAT
^----//--no need this
to
AND ribbons.name LIKE CONCAT
Related
I would like to execute such a command:
SELECT number.phone_number
FROM number
LEFT JOIN ordered_number
ON ordered_number.number_id=number.id
WHERE ordered_number.order_id=123
with Yii2. I do this:
$numery = Number::find()
->select('number.phone_number')
->leftJoin('ordered_number', ['ordered_number.number_id' => 'number.id'])
->where(['ordered_number.order_id' => 123])
->createCommand()
->rawSql;
But I get then this:
'SELECT `number`.`phone_number` FROM `number` LEFT JOIN `ordered_number` ON `ordered_number`.`number_id`='number.id' WHERE `ordered_number`.`order_id`=123'
Which doesn't work. When I put the first thing right into my base, I get 4 results, which is correct. But when I do (I think exactly the same) with Yii, I have troubles, because it is null. The only differences between what I have written and what appears after rawSql, are ` and '.
Sorry, I've just read docs for this method - it should be:
->leftJoin('ordered_number', 'ordered_number.number_id = number.id')
because using the where() kind of conditions with array leads to comparing field to string.
Maybe this link could help you a lot , the comments of ActiveRecord.
I'm using the following query to join two tables. Table gz_topics features basic information, such as page Title and Subtitle, while the articles for each page are stored in table gz_articles_topics_intro. GZ.URL is actually a variable that matches each page's URL, but I'm using "Birds" here as an example.
$Zext = mysql_fetch_assoc(mysql_query("SELECT GZ.N, GZ.URL, GZ.Title, GZ.Live,
AI.URL, AI.Article, AI.Pagedex
FROM gz_topics GZ
LEFT JOIN gz_articles_topics_intro AI ON AI.URL = GZ.URL
WHERE GZ.URL LIKE 'Birds' AND GZ.Live = 1"));
It works fine. The problems begin when I put my articles in three separate tables and try to join them...
$Zext = mysql_fetch_assoc(mysql_query("SELECT GZ.N, GZ.URL, GZ.Title, GZ.Live,
Art.URL, Art.Article, Art.Pagedex,
AI.URL, AI.Article, AI.Pagedex, AN.URL, AN.Article, AN.Pagedex
FROM gz_topics GZ
LEFT JOIN gz_articles_topics Art ON Art.URL = GZ.URL
LEFT JOIN gz_articles_topics_intro AI ON AI.URL = GZ.URL
LEFT JOIN gz_articles_topics_names AN ON AN.URL = GZ.URL
WHERE GZ.URL LIKE '$MyURL' AND GZ.Live = 1"));
When I paste it into SQL, it works just fine, displaying all the data, including the article. But no values display on my page.
This is the code I use to display the article:
$Article = $Zext['Article'];
echo $Article;
I've tried inner joins and outer joins, while loops, etc., but nothing seems to work. My PHP/MySQL skills are intermediate, and I don't have a clue what the problem is because, as I said, it works fine when I paste it into SQL, and I don't see any error messages.
The problem must be right under my nose, because this looks pretty simple - even for me. ;)
Thanks for any tips.
On edit: I fixed one mistake, and my query is now displaying items from the table gz_topics, like the page title. However, it still doesn't display the article.
Problem 1) You have three columns with the same name "Article"
Art.Article, AI.Article, AN.Article
You may rename them like this:
Art.Article AS Article1, AI.Article AS Article2, AN.Article AS Article3
And then you can use the appropriate column (1, 2 or 3):
$Article = $Zext['Article1'];
Problem 2) If the contents are plain text (not HTML), you should escape the output:
echo htmlentities($Article);
With the help of this community, I was able to produce a query using an inner join which I thought would work for what I'm trying to do. Unfortunately, when I attempted to run the query found below, I received the following error:
ERROR: table name "bue" specified more than once
From what I've read on Google, some people have said that the "FROM bue" is not needed, but when I removed this, I got another error found below:
ERROR: syntax error at or near "INNER" at character 98
I'd very much appreciate your assistance in troubleshooting this. Thank you so very much.
Query:
UPDATE
bue
SET
rgn_no = chapterassociation.rgn_no,
chp_cd = chapterassociation.chp_cd
FROM
bue
INNER JOIN
chapterassociation
ON bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd
WHERE
bue.mbr_no IS NULL AND bue.chp_cd IS NULL
In PostgreSQL, specifying the table to be updated needs to be done only in the UPDATE clause, e.g. UPDATE bue. The FROM clause is only for additional tables referenced in the query. (If you were doing a self-join on bue, you would mention it again in the FROM clause, but you aren't in this case.)
The second error you get is likely just a simple syntax error. The other tricky thing is that JOIN/ON syntax doesn't fit in the FROM clause, so you have to move the join conditions to the WHERE clause. Try something like:
UPDATE
bue
SET
rgn_no = chapterassociation.rgn_no,
chp_cd = chapterassociation.chp_cd
FROM
chapterassociation
WHERE
bue.mbr_no IS NULL AND bue.chp_cd IS NULL
AND bue.work_state = chapterassociation.work_state
AND bue.bgu_cd = chapterassociation.bgu_cd
See http://www.postgresql.org/docs/current/interactive/sql-update.html.
(N.B. At least I don't know how to put JOIN/ON into an UPDATE statement... I could be missing something.)
OK so I am retrieving fullname from web page to filter customer list and I am not really sure which part is first name or last name so I need to run a similar query like the one below:
"SELECT sc.*, c.firstname, c.lastname,c.email FROM scoop_customer AS sc LEFT JOIN
customer AS c ON sc.customer_id = c.customer_id WHERE c.firstname + ' ' + c.lastname
LIKE '%".$fullname."%'"
But it doesn't seem working to me even though I tried many times and normally it should have been returning values from DB. Could you please tell me where I am doing wrong?
Try CONCAT
WHERE concat(c.firstname, ' ', c.lastname) LIKE '%".$fullname."%'"
Use CONCAT(c.firstname , ' ' , c.lastname )
CONCAT Would be nice for these operations
Please see the mysql doc
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat
"SELECT sc.*, c.firstname, c.lastname,c.email FROM scoop_customer AS sc LEFT JOIN
customer AS c ON sc.customer_id = c.customer_id WHERE CONCAT(c.firstname, ' ', c.lastname) LIKE '%".mysql_real_escape_string($fullname)."%'"
I'd break full name into first and last and do 2 like checks and get rid of the Wildcard % at the beginning of each. The concatenation and the wildcard starting the like is going to break any indexing you may have on the name fields.
Of course if you only have a couple thousand customers in the table the indexing won't mater much, but when you get into the 100,000's you'll feel it!
Asuming that you haven't escaped it previously I have added mysql_real_escape_string.
For the question, the trick is OR, but not CONCAT
".... WHERE c.firstname LIKE '%".mysql_real_escape_string($fullname)."%'
OR c.lastname LIKE '%".mysql_real_escape_string($fullname)."%'"
I've a simple question: how can I use CodeIgniter's ActiveRecord join function? I want this:
LEFT JOIN cimke ON (mk_terem.id_kicsoda=5 AND mk_terem.id_target=cimke.id_cimke)
LEFT JOIN tanar ON (mk_terem.id_kicsoda=1 AND mk_terem.id_target=tanar.id_tanar)
But if I use $this->db->join(cimke,"mk_terem.id_kicsoda=5 AND mk_terem.id_target=cimke.id_cimke"), the value 5 will be between backticks.
How can I do this?
UPDATE
What I want? If mk_terem.id_kicsoda is 1, then I want tanar.nev and when mk_terem.id_kicsoda is 5, I want cimke.nev.
The full SQL-query:
SELECT
terem.nev terem_nev,
elem_tipus.nev tipus_nev,
(IFNULL(cimke.nev,tanar.nev)) nev
FROM mk_terem
LEFT JOIN terem ON mk_terem.id_terem=terem.id_terem
LEFT JOIN cimke ON (mk_terem.id_kicsoda=5 AND mk_terem.id_target=cimke.id_cimke)
LEFT JOIN tanar ON (mk_terem.id_kicsoda=1 AND mk_terem.id_target=tanar.id_tanar)
LEFT JOIN elem_tipus ON (mk_terem.id_kicsoda=elem_tipus.id_kicsoda)
Only a simple workaround, ugly, not elegant, but it works in this case:
$original_reserved = $this->db->_reserved_identifiers;
$this->db->_reserved_identifiers[] = 5;
$this->db->_reserved_identifiers[] = 1;
// or any other values
$this->db->join('with critical values and conditions');
// some db-stuff
$this->db->_reserved_identifiers = $original_reserved;
If anybody knows better please show it!
$this->db->join('cimke', 'mk_terem.id_target = cimke.id_cimke');
$this->db->join('tanar', 'mk_terem.id_target = tanar.id_tanar');
$this->db->where('mk_terem.id_kicsoda', 5);
$this->db->where('mk_terem.id_kicsoda', 1);
or use
$this->db->query('AND_YOUR_RAW_SQL_QUERY_HERE');
Another simple solution would be to temporarily set the protect_identifiers off like so:
$this->db->_protect_identifiers = false;
After making the query you could set it back to true
I know i am very answering this question very late. Just i am sharing my knowledge. It may help us to known something. If I am wrong, Please tell me. I am answering this question by the full query you posted on your question. Kindly check it below.
$this->db->from('mk_terem');
$this->db->select('terem.nev terem_nev, elem_tipus.nev tipus_nev, (IFNULL(cimke.nev,tanar.nev)) nev');
$this->db->join('terem','mk_terem.id_terem=terem.id_terem','left');
$this->db->join('cimke','(mk_terem.id_kicsoda=5 AND mk_terem.id_target=cimke.id_cimke)','left');
$this->db->join('tanar','(mk_terem.id_kicsoda=1 AND mk_terem.id_target=tanar.id_tanar)','left');
$this->db->join('elem_tipus','(mk_terem.id_kicsoda=elem_tipus.id_kicsoda)','left'); $this->db->get();
I got the query which you posted in the question as full Query. Check it below.
SELECT `terem`.`nev` terem_nev, `elem_tipus`.`nev` tipus_nev, (IFNULL(cimke.nev, `tanar`.`nev))` nev FROM (`mk_terem`) LEFT JOIN `terem` ON `mk_terem`.`id_terem`=`terem`.`id_terem` LEFT JOIN `cimke` ON `mk_terem`.`id_kicsoda`=`5` AND mk_terem.id_target=cimke.id_cimke) LEFT JOIN `tanar` ON `mk_terem`.`id_kicsoda`=`1` AND mk_terem.id_target=tanar.id_tanar) LEFT JOIN `elem_tipus` ON `mk_terem`.`id_kicsoda`=`elem_tipus`.`id_kicsoda)`
I believe everything is possible to do by codeignitor Active record. No need to execute the query in $this->db->query() as raw sql.
#uzsolt: I am explaining this because of the argument you made under the topic "http://stackoverflow.com/questions/8344769/writing-sql-queries-in-codeigniter-2-0". And I will try your other bugs you posted under the above URL too shortly and get back to you. If i am anything wrong, kindly let me know. Thanks. :)
If you swap the conditions around AND, it will work! CodeIgniter only escapes the first part of the condition.
So, inthis should work:
$this->db->join('cimke',"mk_terem.id_kicsoda = 5 AND mk_terem.id_target = cimke.id_cimke", "left")